Codeforces Round #412 C. Success Rate
C. Success Rate
2 seconds
256 megabytes
standard input
standard output
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made ysubmissions, 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?
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.
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.
4
10
0
-1
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 to9 / 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) = p/q 来表示其核心思想,其中a 为做对的题目,b为做的题目,则有x+a = k*p,y+b = k*q.且有0<=a<=b,两式合并可得k>=x/p,k>=(y-x)/(q-p), 则如要满足两个等式,k应取其中较大的值(注意:若取得的k为小数,我们需要的是最小整数解,加1),最后注意一下特殊情况 p = 0,p = q.
实现代码:
#include <bits/stdc++.h>
using namespace std; int main() {
long long n, x, y, p, q, k;
cin >> n;
for (int i = ; i < n; i++) {
cin >> x >> y >> p >> q;
if (p == ) {
if (x == ) cout << << endl;
else cout << - << endl;
}
else if (p == q) {
if (x == y) cout << << endl;
else cout << - << endl;
}
else {
long long k1 = (y-x)/(q-p) + (((y-x)%(q-p)) ? : );
long long k2 = x/p + ((x%p) ? : );
k = max(k1,k2);
cout << k*q-y << endl;
}
}
return ;
}
Codeforces Round #412 C. Success Rate的更多相关文章
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)
A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces Round#412 Div.2
A. Is it rated? 题面 Is it rated? Here it is. The Ultimate Question of Competitive Programming, Codefo ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心
A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #412 Div. 2 第一场翻水水
大半夜呆在机房做题,我只感觉智商严重下降,今天我脑子可能不太正常 A. Is it rated? time limit per test 2 seconds memory limit per test ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?
地址:http://codeforces.com/contest/807/problem/C 题目: C. Success Rate time limit per test 2 seconds mem ...
- Codeforces Round #412 A Is it rated ?
A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes Is it rated? Her ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring
地址:http://codeforces.com/contest/807/problem/D 题目: D. Dynamic Problem Scoring time limit per test 2 ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #412 B. T-Shirt Hunt
B. T-Shirt Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- 阿里巴巴Java开发规约插件p3c详细教程及使用感受 - 转
http://www.cnblogs.com/han-1034683568/p/7682594.html
- Postgres使用ALTER USER命令修改用户的密码、密码过期,锁定,解锁
使用ALTER USER命令修改用户的密码.密码过期,锁定,解锁 (1)修改用户的口令,将用户的口令修改为新的密码 highgo=#create user test with password ‘te ...
- Unity接入Steamworks
一.将scrpts/Steamworks.net/SteamManager组件添加到游戏物体上 二.修改SteamManager的代码为游戏的id如图所示 三.Unity,打开项目根目录,修改stea ...
- Visual Studio 2013编辑器+SourceTree代码管理工具及扩展工具
Visual Studio 2013: 美国微软公司的编辑开发工具 扩展工具: Resharper:进行深度代码分析,函数深度查询(ctrl+鼠标左): Grunt:是基于Node.js的项目以自动化 ...
- github的使用心得
我的github地址:https://github.com/gaino1/test GitHub 是一个用于使用Git版本控制系统的项目的基于互联网的存取服务. GitHub可以托管各种git库,并提 ...
- is interest important?
学习是不是一定要看兴趣呢?高中时觉得只要肯学即使不喜欢又能如何,大学之后被深深打脸,面对一周那么多的实习和报告,我悄悄告诉自己不是这块料 有一些事情我就是学不会.我却很容易相信一个人. 因此,无论我如 ...
- 重载(overload)、覆盖(override)、隐藏(hide)的区别
http://blog.csdn.net/yanjun_1982/archive/2005/09/02/470405.aspx 重载是指不同的函数使用相同的函数名,但是函数的参数个数或类型不同.调用的 ...
- Flask-论坛开发-1-基础知识
对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. 第一个 flask 程 ...
- [2017BUAA软工]第0次博客作业
第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 当初选择计算机专业作为自己报考大学的第一志愿,主要是看重了市场对于计算机行业人士的巨大需求,同时也感慨于计算机行 ...
- PAT 1028 人口普查
https://pintia.cn/problem-sets/994805260223102976/problems/994805293282607104 某城镇进行人口普查,得到了全体居民的生日.现 ...