CF 412 C. Success Rate

Source: Internet
Author: User

Original question:
C. Success Rate
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
You is an experienced codeforces user. Today you found out this during your activity on codeforces you had made Y submissions, out of which X has been SUCCESSF Ul. Thus, your current success rate on Codeforces are equal to X/Y.

Your favorite rational number in the [0;1] range is p/q. Now you wonder:what are the smallest number of submissions you had to make if you want your success?

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 x, y, p and Q (0≤x≤y≤109; 0≤p≤q≤109; y > 0; q > 0).

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

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

Output
For each test case, output a single integer equal to the smallest number of submissions that you had to make if you want your Success rate to being equal to your favorite rational number, or-1 if the 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
Note
In the first example, you has to make 4 successful submissions. Your success rate is equal to 7/14, or.

In the second example, you had to make 2 successful and 8 unsuccessful submissions. Your success rate is equal to 9/24, or 3/8.

In the third example, there are no need to do any new submissions. Your success rate was already equal to 20/70, or 2/7.

The fourth example, the only unsuccessful submission breaks your hopes, has the success rate equal to 1.

English:
Give you four number x,y,p,q,x indicates the number of victories, Y indicates the total number of matches. Now ask you at least how many times, to make X/y equals p/q

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf=9223372036854775807;

int main ()
{
    Ios::sync_with_stdio (false);
    ll X,y,p,q;
    int t;
    cin>>t;
    while (t--)
    {
        cin>>x>>y>>p>>q;

        ll num,den,tot=0;
        ll mid,l=0,r=1000000000;
        ll Ans=inf;
        while (r-l>=0)
        {
            mid= (r+l)/2;
            num=mid*p;
            Den=mid*q;
            if (num>=x)
            {
                tot=num-x;
                if (den-tot>=y)
                {
                    tot+= (den-y-tot);
                    Ans=tot;
                    r=mid-1;
                    Continue;
                }
            }
            l=mid+1;
        }
        if (ans==inf)
            cout<<-1<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

Answer:

The last X/y must become (n p)/(n Q) So, the size of the binary n, go to construct (n p)/(n Q), then subtract x and Y.
This topic should also be implemented in a purely mathematical way ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.