QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738196 | #9622. 有限小数 | huo_hua_ya# | WA | 40ms | 3856kb | C++23 | 1.7kb | 2024-11-12 18:13:22 | 2024-11-12 18:13:23 |
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 = 1e18, 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);
// cerr << (long long)l <<'\n';
long long cx = c,tx = t;
for(int i=1;i<=3000;i++){
check(l + i);
// cerr <<c <<'\n';
if(c == 0)break;
if(cx > c&&t*p<=1000000000ll&&t%2==0&&t%5==0){
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
4 1 2 2 3 3 7 19 79
output:
0 1 1 3 1 14 3 316
result:
ok 4 case(s)
Test #2:
score: -100
Wrong Answer
time: 40ms
memory: 3856kb
input:
10000 11 12 28 53 17 60 2 35 17 181 80 123 68 141 79 163 71 99 13 64 33 61 15 32 16 61 11 86 33 74 128 143 40 53 7 23 30 31 5 6 86 181 73 91 13 23 71 81 1 2 7 38 117 160 33 83 129 151 88 153 25 58 16 19 19 141 95 124 43 96 71 139 11 59 106 109 93 152 34 43 17 99 1 57 20 159 16 25 5 73 159 170 172 17...
output:
4 3 8 1590 0 27 1 7 6 30770 43 123 5 282 5 326 14 4950 7 2 11 1220 7 6 8 1830 5 129 2 37 7 1430 13 53 2 69 1 31 2 3 9 362 18 91 8 230 10 81 0 1 3 190 2 20 2 830 4 16610 35 19890 2 29 3 19 8 987 16 930 8 60 4 12510 4 295 3 109 5 19 4 430 13 3960 0 228 19 1113 3 1 3 1022 11 170 7 179 6 130 2 119 7 172...
result:
wrong answer Jury found better answer than participant's 1 < 4 (Testcase 1)