QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#736439 | #9622. 有限小数 | xianggui | RE | 0ms | 0kb | C++14 | 1.6kb | 2024-11-12 11:05:49 | 2024-11-12 11:05:50 |
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
}
// bool getans(int cur2) {
// int x, y;
// exgcd()
// }
void solve() {
int a, b;
scanf("%lld%lld", &a, &b);
int cur = 1;
int ans = 1000000000, ans2 = 0;
// int
auto getans = [&b, &a](int cur2) -> int {
int x, y;
// cerr << b << ' ' << 1 << '\n';
exgcd(b, 1, x, y);
y = -y;
// cerr << x << ' ' << y << '\n';
x *= cur2 * a;
y *= cur2 * a;
// cerr << x << ' ' << y << '\n';
y %= b;
y += b;
y %= b;
// cerr << y << '\n';
return y;
};
for (int i = 0; i <= 20; ++i) {
// int cu
int cur2 = cur;
for (int j = 0; j <= 20; ++j) {
// if (cur2 == 2) cerr << 1 <000000000< '\n';
int tmp = getans(cur2);
// if (cur2 == 2) cerr << tmp << '\n';
if (tmp < ans) {
// cerr << cur2 << ' ' << tmp << '\n';
ans = tmp;
ans2 = cur2 * b;
}
// ans = min(ans, getans(cur2));
if (cur2 > 1000000000 / (5 * b)) break;
cur2 *= 5;
}
if (cur > 1000000000 / (2 * b)) break;
cur *= 2;
}
printf("%lld %lld\n", ans, ans2);
}
signed main() {
int T;
scanf("%lld", &T);
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
4 1 2 2 3 3 7 19 79