QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#737318 | #9622. 有限小数 | crychic | WA | 34ms | 3868kb | C++17 | 1.6kb | 2024-11-12 15:28:06 | 2024-11-12 15:28:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll dd;
void exgcd(ll a,ll b,ll &x,ll &y){
if(b == 0){
x = 1;
y = 0;
dd = a;
return ;
}
exgcd(b,a % b,y,x);
y -= a / b * x;
}
map<ll,int> mp;
ll dfs_mod;
// (ad + bc) / bd (b * y + x * dfs_mod) == obj
ll ans = 1e18,ansd = -1;
ll a,b;
void dfs(ll nowd){
if(mp.count(nowd))return ;
if(nowd > 1000) return ;
mp[nowd] = 1;
ll obj =(((- a * nowd) % dfs_mod) + dfs_mod) % dfs_mod;
ll x,y;
exgcd(b,dfs_mod,x,y);
if(obj % dd == 0){
ll k = obj / dd;
ll res = ((k * x % dfs_mod) + dfs_mod) % dfs_mod;
if(res < ans){
ans = res;
ansd = nowd;
}
// cout << dfs_mod << ' ' << k << ' ' << nowd << ' ' << res << ' ' << obj << '\n';
}
dfs(nowd * 2);
dfs(nowd * 5);
}
void solve(){
cin >> a >> b;
vector<ll> facs;
ans = 1e18;
mp.clear();
for(int i = 1;i * i <= b;i ++){
if(b % i == 0){
if(i % 2 != 0 && i % 5 != 0){
facs.push_back(i);
}
int inv = b / i;
if(inv != i && inv % 2 != 0 && inv % 5 != 0){
facs.push_back(inv);
}
}
}
ll val = b;
while(val % 2 == 0)val /= 2;
while(val % 5 == 0) val /= 5;
for(auto fac : facs){
dfs_mod = val * fac;
dfs(fac);
}
cout << ans << ' ' << ansd << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while(t --){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3588kb
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: 34ms
memory: 3868kb
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:
1 3 19 265 7 6 3 560 96 905 43 123 5 282 5 326 13 396 0 1 18 305 0 1 23 610 967 172 793 296 15 143 12 265 3 368 1 31 7 6 9 362 18 91 4 115 10 81 0 1 226 475 0 1 1 415 22 151 19 765 533 580 1 608 46 705 88 310 7 3 62 695 1 944 3 109 249 152 2 215 14 495 41 912 59 795 0 1 23 730 209 340 7 179 109 52 2...
result:
wrong answer Jury found better answer than participant's 1 < 19 (Testcase 2)