思路:
水题;
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>‘9‘||Cget<‘0‘) Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } int main() { ll t,x,y,p,q; in(t); for(;t--;) { in(x),in(y),in(p),in(q); p/=gcd(p,q),q/=gcd(p,q); if(q==p) { if(x!=y) { printf("-1\n"); continue; } } if(p==0) { if(x!=0) { printf("-1\n"); continue; } } ll pos=(q-y%q)%q; double ki=(double)p/(double)q,li,ri; ll l=0,r=1000000000,ans; while(l<=r) { int mid=l+r>>1; li=(double)x/(double)(y+pos+(mid*q)); ri=(double)(x+pos+mid*q)/(double)(y+pos+mid*q); if(ki>=li&&ki<=ri) ans=mid,r=mid-1; else l=mid+1; } printf("%lld\n",pos+ans*q); // cout<<pos+ans*q; // putchar(‘\n‘); } return 0; }
时间: 2024-11-05 12:19:14