Codeforces Round #412 C. Success Rate

Success Rate

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

43 10 1 27 14 3 820 70 2 75 6 1 1

Output

4100-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.

由题可知,要满足(x+a)/(y+b) == np/nq  ,所以(x+a)=np , (y+b)=nq。由于0 <= a <= b,所以n>=x/p,x>=(y-x)/(q-p) 向上取整求满足条件的最小n值,

就可得答案是b=nq-y;

p==q 和p==0特判下

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 ll x, y, q, p;
 5 int main(){
 6     int t;
 7     cin >> t;
 8     while(t--){
 9         cin >> x >> y >> p >> q;
10         if(q == p){
11             printf("%d\n",(x==y)?0:-1);
12             continue;
13         }
14         if(p == 0){
15             printf("%d\n",(x==0)?0:-1);
16             continue;
17         }
18         ll GCD = __gcd(q,p);
19         q/=GCD; p/=GCD;
20         ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
21         printf("%lld\n",n*q-y);
22     }
23     return 0;
24 }
时间: 2024-10-18 03:17:24

Codeforces Round #412 C. Success Rate的相关文章

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participant

VK Cup 2017 Round 3 + Codeforces Round #412

A 读题题 B 就是你排名第p,得了x分,而最终至少需要y分,你需要最少的successful hack,使得最终得分s>=y,且由s随机取25个数,使p被选中. (1)暴力枚举hack成功几次,失败几次就好了 (2)另解:枚举尽可能小的s,使|s-x|=0(mod 50),分类讨论 If s?≤?x, we need 0 successful hacks, since we can just make (x?-?s)?/?50 unsuccessful hacks. If s?>?x and

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2k. Let's call that representation prairie partition of x. For example, the p

Codeforces Round #412 Div.2

A.模拟 1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 inline void read(int &ans) { 6 static char ch = getchar(); 7 register int neg = 1; 8 ans = 0; 9 for (; !isdigit(ch); ch = getchar()) 10 if (ch == '-') neg = -1; 11 for

AC日记——Success Rate codeforces 807c

Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

cf之路,1,Codeforces Round #345 (Div. 2)

 cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....       其实这个应该是昨天就写完的,不过没时间了,就留到了今天.. 地址:http://codeforces.com/contest/651/problem/A A. Joysticks time limit per test 1 second memory limit per test 256

CodeForce-807C Success Rate(二分数学)

Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a<=b. (0?≤?x?≤?y?≤?109; 0?≤?p?≤?q?≤?109; y?>?0; q?>?0) 解法: (x+a)/(y+b)==p/q; --> x+a=np; y+b=nq; --> a=np-x; b=nq-y; --> 二分n; #include <cs

Codeforces Round #282 (Div. 2) c

/**  * @brief Codeforces Round #282 (Div. 2) c  * @file c.cpp  * @author mianma  * @created 2014/12/16 16:09  * @edited  2014/12/16 16:09  * @type math  * @note  */ #include <fstream> #include <iostream> #include <cstring> #include <c