QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738104 | #9622. 有限小数 | Williamtym | TL | 0ms | 0kb | C++20 | 1.6kb | 2024-11-12 17:44:15 | 2024-11-12 17:44:20 |
answer
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
LL read()
{
LL x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void write(LL x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
set<LL> s;
void init()
{
for(int i = 1;i <= 1e9; i *= 2)
for(int j = i;j <= 1e9; j *= 5)
s.insert(j);
}
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(!b)
{
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - a / b * x;
return d;
}
void solve()
{
LL a = read(), b = read();
if(s.count(b))
{
cout << 0 << ' ' << 1 << endl;
return;
}
while(b % 2 == 0)
b /= 2;
while(b % 5 == 0)
b /= 5;
LL minv = 1e9;
LL u, v;
for(auto i : s)
{
u = (b - a * i % b) % b;
if(minv > u)
{
minv = u;
v = b * i;
//cout << minv << endl;
}
}
cout << minv / __gcd(minv, v) << ' ' << v / __gcd(minv, v) << endl;
}
int main()
{
init();
LL T = read();
while(T --)
{
solve();
}
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
4 1 2 2 3 3 7 19 79