QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738213 | #9622. 有限小数 | ucup-team059# | TL | 1ms | 3624kb | C++20 | 1.4kb | 2024-11-12 18:16:19 | 2024-11-12 18:16:21 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
ll exgcd(ll a, ll b, ll &x, ll &y){
if (b == 0){
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
void solve(){
int a, b;
std::cin >> a >> b;
std::vector<ll> num;
if (b == 5 || b == 2){
std::cout << "0 1\n";
return;
}
constexpr int MAX = 1e6;
for (ll i = 1; i <= MAX; i *= 2){
for (ll j = 1; i * j <= MAX; j *= 5){
for (ll k = 1; i * k * j <= MAX; k *= b){
num.push_back(i * k * j);
}
}
}
std::sort(num.begin(), num.end());
std::pair<ll, ll> ans = {1e18, 1e18};
for (auto d : num){
// std::cout << d << '\n';
ll num1 = a * d;
if (num1 % b == 0){
ll by = b;
ll t = d;
while (t % b == 0){
by *= b;
t /= b;
}
ll add = by;
ll x, y;
if (num1 % std::gcd(by, b)){
continue;
}
ll dd = exgcd(by, b, x, y);
y = -y;
while (x < 0 || y < 0){
x += b / dd;
y += by / dd;
}
while (x >= b / dd && y >= by / dd){
x -= b / dd, y -= by / dd;
}
by = x * by;
by -= num1;
by /= b;
if (ans > std::make_pair(by, d) && by >= 0){
ans = std::make_pair(by, d);
}
}
}
std::cout << ans.first << ' ' << ans.second << '\n';
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3624kb
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
Time Limit Exceeded
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...