QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738145 | #9622. 有限小数 | huo_hua_ya# | WA | 1ms | 3572kb | C++23 | 1.6kb | 2024-11-12 17:52:55 | 2024-11-12 17:53:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 7;
const int mod = 998244353;
#define ll __int128
//求解不定方程 ax+by=c 的最小整数解 x
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b) {x = 1; y = 0; return a;}
ll ans = exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - a / b * y;
return ans;
}
ll x, y;
ll cal(ll a, ll b, ll c)
{
x = 0, y = 0;
ll ans = exgcd(a, b, x, y);
if (c % ans)return -1;
x *= c / ans; b /= ans;
// y *= c / ans;
if (b < 0)b = -b;
x = (x % b + b) % b;
return x;
}
void solve()
{
int a, b;
cin >> a >> b;
ll p = b, k = 1;
while (p % 2 == 0) {
p /= 2;
k *= 2;
}
while (p % 5 == 0) {
p /= 5;
k *= 5;
}
long long c = 0, t = 0;
auto check = [&](ll id) {
c = cal(k, a, p * id);
// cerr << c << "\n";
t = (id*p - 1ll * k * c) / a;
return t >= 1;
};
ll l = 1, r = 1e18;
while (l < r) {
ll mid = (l + r) >> 1;
if (check(mid))r = mid;
else l = mid + 1;
}
check(l);
long long cx = c,tx = t;
for(int i=1;i<=3000;i++){
check(l + i);
if(cx > c&&t*p<=1000000000){
cx = c;
tx = t;
}
}
long long d = tx * p;
// assert(d<=1000000000&&d>=1);
cout << cx << " " << d << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3572kb
input:
4 1 2 2 3 3 7 19 79
output:
0 1 0 9 0 49 0 6241
result:
wrong answer The result is not terminating.(Testcase 2)