CF Round #412( Div.2) Success Rate

C. Success Rate

time limit per test 2 seconds

memory limit per test256 megabytes


You are an experienced Codeforces user.Today you found out that during your activity on Codeforces you have madey submissions, outof which x have been successful. Thus, yourcurrent success rate on Codeforces is equal to x / y.

Your favorite rational number in the[0;1] range is p / q. Now youwonder: what is the smallest number of submissions you have to make if you wantyour success rate to bep / q?

Input

The first line contains a single integert (1 ≤ t ≤ 1000) — thenumber of test cases.

Each of the nextt lines containsfour integers x,y,p and q (0 ≤ x ≤ y ≤ 109;0 ≤ p ≤ q ≤ 109;y > 0;q > 0).

It is guaranteed thatp / q is anirreducible fraction.

Hacks. For hacks, anadditional 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 rateto be equal to your favorite rational number, or-1 if this isimpossible 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 have to make 4successful submissions. Your success rate will be equal to7 / 14, or 1 / 2.

In the second example, you have to make2 successful and 8 unsuccessful submissions. Your success rate will be equal to9 / 24, or 3 / 8.

In the third example, there is no needto make any new submissions. Your success rate is already equal to20 / 70, or 2 / 7.

In the fourth example, the only unsuccessful submissionbreaks your hopes of having the success rate equal to 1.


题意:现在有一个AC Ratio(分数形式,AC题数/总提交题数),问是否能够通过最小的提交数(每次提交可以是正确,也可以是错误)使AC Patio 达到一个最简分数

这道题开始想要暴力搞,果断超时,后来列出等式(x+a)/(y+a+b)=p/q,化简得a=(py-qx+pb)/(q-b),求出最小的a,再加上b即可,我利用中国剩余定理来求最小的a,然而不是RE就是TLE。。。后来发现不用那么复杂


法一:直接求需要把第二个分数扩大的倍数


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 1005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

int main()
{
    ll x,y,p,q;
    rush()
    {
        scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
        if(p==q)
        {
            if(x==y)
                puts("0");
            else  puts("-1");
            continue;
        }
        if(p==0)
        {
            if(x==0)
                puts("0");
            else puts("-1");
            continue;
        }
        ll a=q-p;
        ll b=y-x;
        ll cnt=max((x+p-1)/p,max((y+q-1)/q,(b+a-1)/a));
        ll ans=cnt*q-y;
        printf("%I64d\n",ans);
    }
    return 0;
}

法二:二分

#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 1005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

int main()
{
    ll x,y,p,q;
    rush()
    {
        scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
        ll l=0,r=INF;
        ll m;
        while(l<r)
        {
            m=(l+r)/2;
            if(q*m<y||p*m<x)
            {
                l=m+1;
            }
            else if(q*m-y>=p*m-x)
            {
                r=m;
            }
            else l=m+1;
        }
        if(l==INF)
            puts("-1");
        else printf("%I64d\n",q*l-y);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值