您的位置:首页 > 其它

Codeforces 807C(二分)

2018-01-29 11:44 337 查看
问题描述:

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 x, y, p 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/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"
4000
,-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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: