CF Round #412( Div.2) Success Rate
2017-05-08 01:41
169 查看
C. Success Rate
time limit per test 2seconds
memory limit per test256
megabytes
You are an experienced Codeforces user.Today you found out that during your activity on Codeforces you have madey
submissions, outof which x
have been successful. Thus, yourcurrent success rate on Codeforces is equal to
x / y.
Your favorite rational number in the[0;1]
range is p / q.
Now youwonder: what is the smallest number of submissions you have to make if you wantyour success rate to bep / q?
Input
The first line contains a single integert
(1 ≤ t ≤ 1000) —
thenumber of test cases.
Each of the nextt
lines containsfour integers x,y,p
and q
(0 ≤ x ≤ y ≤ 109;0 ≤ p ≤ q ≤ 109;y > 0;q > 0).
It is guaranteed thatp / q
is anirreducible fraction.
Hacks.
For hacks, anadditional 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 rateto be equal to your favorite rational number, or-1
if this isimpossible 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 4successful submissions. Your success rate will be equal to7 / 14,
or 1 / 2.
In the second example, you have to make2 successful and 8 unsuccessful submissions. Your success rate will be equal
to9 / 24,
or 3 / 8.
In the third example, there is no needto make any new submissions. Your success rate is already equal to20 / 70,
or 2 / 7.
In the fourth example, the only unsuccessful
submissionbreaks your hopes of having the success rate equal to 1.
题意:现在有一个AC Ratio(分数形式,AC题数/总提交题数),问是否能够通过最小的提交数(每次提交可以是正确,也可以是错误)使AC
Patio 达到一个最简分数
这道题开始想要暴力搞,果断超时,后来列出等式(x+a)/(y+a+b)=p/q,化简得a=(py-qx+pb)/(q-b),求出最小的a,再加上b即可,我利用中国剩余定理来求最小的a,然而不是RE就是TLE。。。后来发现不用那么复杂
法一:直接求需要把第二个分数扩大的倍数
#include <bits/stdc++.h> using namespace std; #define mst(a,b) memset((a),(b),sizeof(a)) #define f(i,a,b) for(int i=(a);i<(b);++i) #define ll long long const int maxn = 1005; const int mod = 475; const ll INF = 0x3f3f3f3f; const double eps = 1e-6; #define rush() int T;scanf("%d",&T);while(T--) int main() { ll x,y,p,q; rush() { scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q); if(p==q) { if(x==y) puts("0"); else puts("-1"); continue; } if(p==0) { if(x==0) puts("0"); else puts("-1"); continue; } ll a=q-p; ll b=y-x; ll cnt=max((x+p-1)/p,max((y+q-1)/q,(b+a-1)/a)); ll ans=cnt*q-y; printf("%I64d\n",ans); } return 0; }
法二:二分
#include <bits/stdc++.h>using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 1005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int main()
{
ll x,y,p,q;
rush()
{
scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
ll l=0,r=INF;
ll m;
while(l<r)
{
m=(l+r)/2;
if(q*m<y||p*m<x)
{
l=m+1;
}
else if(q*m-y>=p*m-x)
{
r=m;
}
else l=m+1;
}
if(l==INF)
puts("-1");
else printf("%I64d\n",q*l-y);
}
return 0;
}
相关文章推荐
- cf 156 div.2 D. Mr. Bender and Square
- Codeforces Round 190 div.2 322C 321A Ciel and Robot
- A. Puzzles CodeForces Round #196 (Div.2)
- Codeforces Round #212 (Div.2)
- codeforces round #236 div.2 A
- Codeforces Round #245 (Div. 1) A /(Div.2 C) Xor-tree
- cf#257(Div.2) B. Jzzhu and Sequences
- 【Codeforeces】【#259】【Div.2】
- cf 267 div.2 D Fedor and Essay
- Codeforces #277.5 (Div.2 A~F)
- Codeforces #284 (Div.1 A~E & Div.2 A~E)
- CodeForces Round #288 Div.2
- Codeforces Round #293 (Div.2) 小记
- Codeforces Round #257 div.2 D or 450D Jzzhu and Cities【最短路】
- #290 (div.2) C. Fox And Names
- codeforce Round304 div.2 D
- #309 (div.2) A. Kyoya and Photobooks
- codeforces Round #Pi (div.2) 567ABCD
- BestCoder Round #50 (div.2) hdu5366、5367(dp+动态线段树)
- BestCoder Round #50 (div.2)