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 ...
随机推荐
- 3.《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——检查文件
上面我们已经学过如何创建及操作文件,现在我们再来学习检查内容.当文件太长以至于屏幕一页显示不完时,这显得尤其重要.特殊情形下,如我们在第2.1章节开始部分中使用cat命令将内容展示到屏幕上,但这对于长 ...
- QZEZ第一届“饭吉圆”杯程序设计竞赛
终于到了饭吉圆杯的开赛,这是EZ我参与的历史上第一场ACM赛制的题目然而没有罚时 不过题目很好,举办地也很成功,为法老点赞!!! 这次和翰爷,吴骏达 dalao,陈乐扬dalao组的队,因为我们有二个 ...
- Linux系统特殊变量
系统给定的特殊变量: 变量名 作用 $0 当前脚本的名字 $n 传递给脚本或者函数的参数,n表示第几个参数 $# 传递给脚本或函数的参数个数 $* 传递给脚本或函数的所有参数 $@ 传递给脚本或者函数 ...
- vim命令记录
最近开始用vim作为日常编辑器,由于vim的命令过多,现在记录一下
- 分布式监控系统Zabbix-添加windows监控主机
大多数情况下,线上服务器都是linux系统,但是偶尔也会有些windows机器.下面简单介绍下zabbix添加windows监控机的操作:1)下载windows的zabbix_agent下载地址:ht ...
- 数组-在Shell脚本中的基本使用介绍
Shell脚本在运维工作中是极其重要的,而数组在shell脚本里的运用无论是在循环或运算方面都是非常实用的一个环节.下面是对shell脚本中数组方面一些操作在此进行记录,希望能帮助到有兴趣的朋友~1. ...
- 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...
- Daily scrum 12.21
今天ui组反映了一个数据库数据类型的问题,开发人员在完成任务后再去处理. Member Today’s task 林豪森 与学霸其他小组交流,处理整合问题 宋天舒 修复数据库问题 张迎春 修复数据库问 ...
- 软件工程驻足篇章:第十七周和BugPhobia团队漫长的道别
0x01 :序言 I am a slow walker, but I never walk backwards. 成长于被爱,学着爱人 成长的故事 也是年少的星期六结束的故事 就仿佛我和BugPhob ...
- 【实践报告】Linux实践二
3.编译并安装内核与模块 sudo make bzImage –j3 编译内核 sudo make modules –j3 编译模块 sudo make modules ...