首页 > 代码库 > AC日记——Success Rate codeforces 807c

AC日记——Success Rate codeforces 807c

Success Rate

 

思路:

  水题;

 

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define ll long longinline 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;}

 

AC日记——Success Rate codeforces 807c