C. Success Rate (codeforces)

今天开始补cf的题。遇到二分的题目, 补了这道题才发现自己对二分的理解还是太浅显了。
题目如下:
C. Success Rate

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
Note
In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.

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

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

In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1.
题目大意如下:
有T组数据,每组数据包含四个值,x, y, p, q;
表示的是做y道题, A掉了x道题, 问最少在做多少道题(这些题可以自由控制正确率),可以使A题率等于p/q。
思路:
二分枚举。
首先对题目进行抽象化:实际就是求 (x + A)/ (y + B) = n * p / (n * q);
最小的B就是所求的答案。
本来我是想枚举B的值, 最后才发现B的值不容易枚举,因为要用除法算n的值,不如直接枚举n的值,去倒推A, B的值。在这里要满足条件 A >= 0, B >= 0, B >= A. 对于P == 0 || P == 1的情况再给予特判就可以了。
二分n的值就可以了。
注意在最开始预处理的时候,对x/y千万不能约分。
代码如下:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 1000000000
int T;
long long int x, y, p, q;
long long int l, r;
long long int gcd(long long int a, long long int b)
{
    if(b == 0) return a;
    return gcd(b, a%b);
}
int main()
{
  cin >>T;
  while(T--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    long long int s1, s2;
    s2 = gcd(p, q);
    p /= s2; q /= s2;
    l = 0; r = INF;
    long long int mid;
    if (p == q && x != y) {
        cout << "-1" << endl;
        continue;
    }
    if(p == 0 && x != 0) {
        cout << "-1" << endl;
        continue;
    }
    while (true) {
        mid = (r - l) / 2 + l;
        long long int B = mid * q - y;
        long long int A = mid * p - x;
        if ( A >= 0 && B >= 0 && A <= B) r = mid;
        else l = mid + 1;
        if( r - l <= 0) break;

    }
    printf("%lld\n", r * q - y);
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值