Codeforces 807C(二分)

问题描述:

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 ≤ 1090 ≤ p ≤ q ≤ 109y > 0q > 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/y让我们在给分母加上最小的数字m,于此同时我们可以给分子加上n (0<=n<=m)使得p/q==(x+n)/(y+m)

题目分析:我们二分倍数使得q*base>=y   p*base>=x,(p*base-x)<=(q*base-y),自己画一下就知道了。

p/q=0和1的情况特判,比较特殊。因为极限只能接近不能到达。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;

ll x,y,p,q;
bool check(int base)
{
    if (base*q<y) return false;
    if (x>p*base) return false;
    if ((p*base-x)>(q*base-y)) return false;
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--) {
        scanf("%lld%lld%lld%lld",&x,&y,&p,&q);
        if (p==0) {
            if (x!=0) {
                printf("%d\n",-1);
                continue;
            }
        }
        if (p==q) {
            if (x!=y) {
                printf("%d\n",-1);
                continue;
            }
        }
        ll left=1,right=1e9,mid;
        while (right>=left) {
            mid=(right+left)/2;
            if (check(mid)) right=mid-1;
            else left=mid+1;
        }
        printf("%lld\n",left*q-y);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值