zoukankan      html  css  js  c++  java
  • Codeforces Round #412 C. Success Rate (rated, Div. 2, base on VK Cup 2017 Round 3)

    C. Success Rate
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q?

    Input

    The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.

    Each of the next t lines contains four integers xyp and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0).

    It is guaranteed that p / q is an irreducible fraction.

    Hacks. For hacks, an additional constraint of t ≤ 5 must be met.

    Output

    For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve.

    Example
    input
    4
    3 10 1 2
    7 14 3 8
    20 70 2 7
    5 6 1 1
    output
    4
    10
    0
    -1

    第一题第二题满满的恶意,看样例猜题意系列。

    第三题题意没什么说的,简单讲一下我是怎么想的好了,p和q互质,最后的结果一定是(x+a)/(y+b)=p*k/q*k,即x+a=p*k,y+b=q*k.(可以a<=b)

    想了一下最极限的情况x=999999999 y=999999999 p=1 q=1000000000,k最大也不超过1e9,所以1~1e9二分,找到第一个最小满足判断的k就行了

    比赛时想偷下懒三组for循环暴力(按理来说复杂度够了),预测时过了也就没多想,结果忘记还有个多组测试的t<=1000,赛后果断TLE,哎,忧伤!

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define max(x,y) (x)>(y)?(x):(y)
    #define min(x,y) (x)>(y)?(y):(x)
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1<<28;
    const int maxn=1010;
    const int maxm=110;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    ll x,y,p,q;
    bool judge(int k)
    {
        if(k*p-x <= k*q-y&&k*p-x>=0&&k*q-y>=0)
            return true;
        return false;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int ncas;
        scanf("%d",&ncas);
        while(ncas--)
        {
            scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
            if(x*q==p*y) {printf("0
    ");continue;}
            ll l=1,r=1e9;
            if(!judge(r)) {printf("-1
    ");continue;}
            while(l<r)
            {
                int mid=(l+r)/2;
                if(judge(mid))
                    r=mid;
                else
                    l=mid+1;
                if(l==r) break;
            }
            printf("%I64d
    ",l*q-y);
        }
        return 0;
    }
  • 相关阅读:
    图01--[基本概念&&图的实现]
    并查集01--[Quick Find&&Quick Union]
    排序05--[计数排序&&基数排序&&桶排序]
    排序04--[快速排序&&希尔排序]
    harukaの赛前日常
    harukaの收藏夹
    hello world&Restart the Journey
    成外集训小记
    CSPS2019游记
    博弈论初步
  • 原文地址:https://www.cnblogs.com/mgz-/p/6825002.html
Copyright © 2011-2022 走看看