AC日记——Success Rate codeforces 807c

Success Rate

思路:

  水题;

代码:

#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

AC日记——Success Rate codeforces 807c的相关文章

AC日记——Cards Sorting codeforces 830B

Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 #define INF 0x3f3f3f3f #define maxtree maxn<<2 int n,ai[maxn],val[maxtree],L[ma

AC日记——Broken BST codeforces 797d

D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005

AC日记——T-Shirt Hunt codeforces 807b

T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int p,x,y,ai[27],ans; bool check(int xx) { if(xx<y) return false; int i=(xx/50)%475; for(int j=1;j<=2

CodeForce-807C Success Rate(二分数学)

Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a<=b. (0?≤?x?≤?y?≤?109; 0?≤?p?≤?q?≤?109; y?>?0; q?>?0) 解法: (x+a)/(y+b)==p/q; --> x+a=np; y+b=nq; --> a=np-x; b=nq-y; --> 二分n; #include <cs

Codeforces Round #412 C. Success Rate

Success Rate You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x?/?y. Your f

AC日记——Aragorn&#39;s Story HDU 3966

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10510    Accepted Submission(s): 2766 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

Codeforces 807C - Success Rate

题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再提交几次可以达到目标正确率p/q; 解题思路:假设提交B次,正确A次,那么可以得到(x+A)/(y+B)=p/q,可以推出x+A=k*p,y+B=k*q.那么A=k*p-A,B=K*q-A; 这样我们只需要二分枚举k,判断A,B是否满足(0<=A<=B)即可. 1 #include<iost

AC日记——Dynamic Problem Scoring codeforces 807d

Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 130 int n,ai[maxn][6],ac[6],cnt,all,last1,last2; double map[3][6]; inline void in(i

Codeforces - 773A - Success Rate - 二分

https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<=p/q,错在这些数是离散的,不能由两边异号判定一定存在这个交点. 然后改成枚举d,使得y=d*q,这样就一定是倍数了.然后就是要想清楚了,找不到这样卡在中间的d,其实都是因为d不够大的原因,d够大保证是可以的除非正确率是100%. 然后就是二分的上界,按道理q的最大值是1e9,y的最大值也是1e9,他们的公倍