QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788649 | #9622. 有限小数 | Evan | WA | 181ms | 4200kb | C++23 | 2.3kb | 2024-11-27 17:46:15 | 2024-11-27 17:46:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define gcd __gcd
const int maxd=1E9;
// 扩展欧几里得算法,用于求 x 和 y,使 ax + by = gcd(a, b)
void extendedEuclid(int a, int b, int &x, int &y, int &g) {
if (b == 0) {
x = 1;
y = 0;
g = a;
return;
}
int x1, y1;
extendedEuclid(b, a % b, x1, y1, g);
x = y1;
y = x1 - (a / b) * y1;
}
// 找到最小正整数 c
int findMinC(int a, int x, int k) {
a = ((-a % k) + k) % k; // 将 -a 调整到 [0, k) 范围
int invX, tempY, gcd;
extendedEuclid(x, k, invX, tempY, gcd); // 计算 x 和 k 的最大公约数及模逆
// 检查是否有解
if (a % gcd != 0) {
return -1;
}
// 计算基本解
invX = (invX % k + k) % k; // 确保 invX 是非负的
int c = (invX * (a / gcd)) % k;
if (c < 0) c += k; // 确保 c 是非负的
return c;
}
void solve()
{
int a,b,c=1E18,d=0,c1=0,c2=0;
scanf("%lld %lld",&a,&b);
int k=b;
while(k%2==0){k=k/2;c1++;}
while(k%5==0){k=k/5;c2++;}
if(k==1)
{
printf("0 1\n");return;
}
map<int,int> mp;
priority_queue<int,vector<int>,greater<int>> pq;
mp[b]=1;
pq.push(b);
while(!pq.empty())
{
int x=pq.top();pq.pop();
if(5*x<=maxd&&!mp[5*x])
{
mp[5*x]=1;
pq.push(5*x);
}
if(2*x<=maxd&&!mp[2*x])
{
mp[2*x]=1;
pq.push(2*x);
}
int res=findMinC(a*x/b,1,k);
int m=gcd(res,x);
res=res/m;x=x/m;
if(res>0){
if(res<c){c=res;d=x;}
}
}
for(int i=0;i<=c1;i++)
{
for(int j=0;j<=c2;j++)
{
int x=k*pow(2,i)*pow(5,j);
int y=b/x;
// a/b+c/x y=b/x
// a = k*m-c*y
int res=findMinC(a,y,k);
//printf("!%d\n",res);
int m=gcd(res,x);
res=res/m;x=x/m;
if(res>0&&c>res)
{
c=res;
d=x;
}
}
}
printf("%lld %lld\n",c,d);
}
signed main()
{
int tt;
scanf("%lld",&tt);
while(tt--)
{
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4016kb
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: 181ms
memory: 4200kb
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 12 1 54272 1 60 1 7 1 231680000 23 3936 1 36096000 5 326 1 63360 0 1 1 31232 0 1 1 4880 1 10750 1 18500 1 11714560 1 331250 1 2944 1 31 1 6 1 289600000 1 455000 1 58880 1 51840 0 1 1 304 0 1 1 415 1 19328000 1 765000000 1 4640 1 608 1 72192 3 775 1 48 3 347500 1 944 1 43600 1 76 1 430000 1 6336 1 ...
result:
wrong answer Jury found better answer than participant's 1 < 2 (Testcase 8812)