ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#213287 | #3847. 分数约分 | one_zero_four_zero | 90 | 1545ms | 1268kb | C++11 | 2.0kb | 2024-11-10 11:52:44 | 2024-11-10 13:06:07 |
answer
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int T;
long long a, b, ansA, ansB;
string A, B;
string tmpA = "";
string turnstring(long long x){
string res = "";
while (x){
res.push_back((x % 10) + '0');
x /= 10;
}
reverse(res.begin(), res.end());
return res;
}
long long turnint(string s){
long long res = 0;
for (auto && i : s){
res = (res * 10) + (i ^ '0');
}
return res;
}
bool check(string tmpA, string tmpB){
int pos = 0;
for (int i = 0; i < B.size() && pos < tmpB.size(); i ++){
if (B[i] == tmpB[pos]) pos ++;
}
if (pos != tmpB.size()) return false;
vector<int> eraA(10, 0), eraB(10, 0);
for (auto && i : A) eraA[i - '0'] ++;
for (auto && i : B) eraB[i - '0'] ++;
for (auto && i : tmpA) eraA[i - '0'] --;
for (auto && i : tmpB) eraB[i - '0'] --;
if (eraA != eraB) return false;
return true;
}
void solve(){
long long rem = turnint(tmpA); // A
long long cur = rem;
if (cur % a) return;
cur /= a;
if (cur > 1000000000000000000LL / b) return;
cur *= b; // B
if (rem >= ansA) return;
string tmpB = turnstring(cur);
int pos = 0;
for (int i = 0; i < B.size() && pos < tmpB.size(); i ++){
if (B[i] == tmpB[pos]) pos ++;
}
if (pos != tmpB.size()) return;
int cnt = 0;
for (int i = 0; i < B.size(); i ++) cnt += (B[i] == '0');
bool P = 0;
for (int i = 0; i <= cnt; i ++){
P |= check(tmpA, tmpB);
tmpB = "0" + tmpB;
}
if (!P) return;
ansA = rem, ansB = cur;
}
void dfs(int k){
if (k == A.size()){
if (turnint(tmpA) == 0) return;
solve();
return;
}
tmpA.push_back(A[k]);
dfs(k + 1);
tmpA.pop_back();
dfs(k + 1);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("../data.in", "r", stdin);
freopen("../data.out", "w", stdout);
#endif
scanf("%d", &T);
while (T --){
tmpA = "";
scanf("%lld %lld", &a, &b);
ansA = a, ansB = b;
A = turnstring(a), B = turnstring(b);
long long cur = __gcd(a, b);
a /= cur, b /= cur;
dfs(0);
printf("%lld %lld\n", ansA, ansB);
}
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1268kb
input:
10 295227892 738069730 21284157 63852471 312774536 781936340 82221828 68518190 167752458 55917486 90...
output:
295227892 738069730 21 63 31277456 78193640 822228 685190 167752458 55917486 904 7232 171 1197 24114...
result:
ok 20 numbers
Test #2:
score: 10
Accepted
time: 0ms
memory: 1264kb
input:
10 7231248 1807812 4301415 3441132 1931046 3862092 5184264 3240165 1144774 1717161 298584 2388672 87...
output:
324 81 4301415 3441132 193146 386292 518424 324015 1144774 1717161 298584 2388672 878 7902 370928 29...
result:
ok 20 numbers
Test #3:
score: 10
Accepted
time: 0ms
memory: 1268kb
input:
10 72320000 27120000 110000 11000000 90100000 9010000 270000 75600000 8230000 82300000 17910000 5970...
output:
32 12 1 100 10 1 2 560 2 20 171 57 893 94 60 6 7 70 434 620
result:
ok 20 numbers
Test #4:
score: 10
Accepted
time: 0ms
memory: 1264kb
input:
10 6700512 10050768 6712050 10068075 7172058 10758087 9318720 13978080 71635224 107452836 71643522 1...
output:
670512 1005768 671250 1006875 717258 1075887 931872 1397808 3522 5283 3522 5283 7192128 10788192 717...
result:
ok 20 numbers
Test #5:
score: 10
Accepted
time: 149ms
memory: 1260kb
input:
10 44896548685746979 44896548685746979 76555658957975878 76555658957975878 9999998899899899 99999988...
output:
4 4 5 5 8 8 5 5 2 2 8 8 8 8 1 1 4 4 9 9
result:
ok 20 numbers
Test #6:
score: 10
Accepted
time: 143ms
memory: 1260kb
input:
10 5586978859946748 5586978859946748 5875932326322532 5875932326322532 7877997999879887 787799799987...
output:
4 4 2 2 7 7 9 9 4 4 6 6 8 8 4 4 7 7 4 4
result:
ok 20 numbers
Test #7:
score: 10
Accepted
time: 242ms
memory: 1264kb
input:
10 163163163163163 326326326326326 163163111163111 326326222326222 142847142847 285714285714 2857142...
output:
11111 22222 111111111 222222222 142847142847 285714285714 285714 714285 8 4 432106661661438007 38112...
result:
ok 20 numbers
Test #8:
score: 10
Accepted
time: 801ms
memory: 1264kb
input:
10 142847114287112857 285714228574225714 142847111111112857 285714222222225714 142847111111111111 28...
output:
142847114287112857 285714228574225714 142847111111112857 285714222222225714 142847111111111111 28571...
result:
ok 20 numbers
Test #9:
score: 10
Accepted
time: 210ms
memory: 1268kb
input:
10 687069149440626451 336182506802090732 216198376277246428 246457923661369649 43706817238884428 372...
output:
687069149440626451 336182506802090732 216198376277246428 246457923661369649 43706817238884428 372089...
result:
ok 20 numbers
Test #10:
score: 0
Time Limit Exceeded
input:
10 142847114247112857 285714228714225714 142847111111112857 285714222222225714 142847111111111111 28...