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
题目分析:我们二分倍数使得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;
}
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 1Output
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;
}
相关文章推荐
- Success Rate CodeForces - 807C 二分
- codeForces 660C.Hard Process(二分+前缀和)
- CodeForces 359 D.Pair of Numbers (二分+ST)
- codeforces 191 E (树状数组+二分)
- CodeForces 378C - Road to Cinema(二分)
- codeforces-729【思维】【二分】
- CodeForces 6E Exposition(二分+RMQ)
- CodeForces - 785C Anton and Fairy Tale(二分)
- Codeforces 703C Chris and Road 二分、思考
- 二分--CodeForces 626C--- Block Towers--
- CodeForces 650B(二分)
- CodeForces-232A【构造 二分】
- CodeForces 493C C. Vasya and Basketball 二分
- Codeforces 348A - Mafia (二分答案)
- Codeforces 479E Riding in a Lift【Dp+前缀和优化+二分】好题~
- Codeforces 750C New Year And Rating 二分判定
- codeforces 350 div2 D Magic Powder - 2 二分
- codeForces 165C.Another Problem on Strings (二分+前缀和)
- Codeforces 660C Hard Process【二分】经典题!好题!
- R2D2 and Droid Army - CodeForces 514 D 二分