Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?
地址:http://codeforces.com/contest/807/problem/C
题目:
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 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?
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.
43 10 1 27 14 3 820 70 2 75 6 1 1
4100-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 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.
思路:略
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int p,x,y; 15 16 int main(void) 17 { 18 //std::ios::sync_with_stdio(false); 19 //std::cin.tie(0); 20 int n; 21 LL x,y,p,q,x1,x2,ans; 22 scanf("%d",&n); 23 for(int i=1;i<=n;i++) 24 { 25 scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q); 26 if(p==0) 27 { 28 if(x!=0) 29 printf("-1\n"); 30 else 31 printf("0\n"); 32 } 33 else if(p==q) 34 { 35 if(x!=y) 36 printf("-1\n"); 37 else 38 printf("0\n"); 39 } 40 else 41 { 42 x1=(q*x-y*p)/p; 43 if(x1*p<(q*x-y*p)) 44 x1++; 45 x2=(y*p-x*q)/(q-p); 46 if(x2*(q-p)<(y*p-x*q)) 47 x2++; 48 ans=max(x1,x2)+y; 49 if(ans%q) 50 ans=((ans/q)+1)*q-y; 51 else 52 ans=ans-y; 53 printf("%I64d\n",ans); 54 } 55 56 } 57 return 0; 58 }
转载于:https://www.cnblogs.com/weeping/p/6824692.html
Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?相关推荐
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)C. Voltage Keepsake
题目链接:C. Voltage Keepsake 题解:二分时间,然后判断看需要充电的时间总和是否在允许的范围内 #include<bits/stdc++.h> #define pb pu ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3)
点击打开A题链接 #include<bits/stdc++.h> using namespace std; const int MAX = 105; int n,s;int h[MAX], ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport
Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...
- VK Cup 2017 - Round 1
传送门 A. Bear and Friendship Condition(思维or完全图判定) •题意 给你n个人,m个朋友关系 朋友是会传递的,若A B是朋友,A C是朋友,则必须有B C的朋友关系 ...
- VK Cup 2017 - Round 2
FallDream打的AB都FFT了,只剩一个我打的C,没进前一百,之后看看马拉松复活赛有没机会呗. A. Voltage Keepsake 题目大意:n个东西,每个东西一开始有bi能源,每秒消耗ai ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)
这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...
- Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))
Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 题号 题目 知识点 A Simply Strange Sor ...
- Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解
Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 文章目录 A. Simply Strange Sort B. ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
A. Doggo Recoloring ps:注意 n == 1 B. Weakened Common Divisor 题解:WCD出现中的数必然是 < a, b >中某个数的公约数.而 ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B. Little Artem and Grasshopper 模拟题...
B. Little Artem and Grasshopper 题目连接: http://www.codeforces.com/contest/669/problem/B Description Li ...
最新文章
- Linux+Qt 下同一数据空间vfork多进程间通信的一种高效便捷方式(信号槽直接调用)
- 当不使用会话状态时禁用它
- oracle中将number类型毫秒值转为时间类型
- IIS下的FTP使用
- linux同步IO: sync、fsync与fdatasync
- 160 - 39 damn
- audio unity 加速_浅谈Unity中Android、iOS音频延迟
- pythonclass实例化_Python中实例化class的执行顺序示例详解
- 程序员的必杀技——编程全能词典
- 20.20 告警系统主脚本;20.21 告警系统配置文件;20.22 告警系统监控项目
- element-plus Table 表格自定义列模板使用及问题
- Keil自动格式化代码
- 状态反馈控制与状态观测器设置以及利用LQR方法求取状态反馈矩阵
- 三菱Fx系列PLC的编程口协议
- 这个英文语法校正插件很赞!可以校正邮件,也可以校正论文
- 冲激函数与卷积的多次邂逅
- Everything+cpolar搭建在线资料库,实现随时随地访问
- missing iso 9660 image the installer has tried to mount image #1
- python+itk+读取dicom数据,并保存为nii文件
- 下载vue模板框架并使用
热门文章
- 苹果Mac应用音频捕获录制工具:​​​​​​​​TunesKit Audio Capture
- 如何在 macOS Monterey 中管理应用程序和联系人的 Mac 通知?
- MWeb Pro for Mac(静态博客生成软件)
- 运维监控三剑客之Zabbix
- git本地分支和远程分支改名
- 使用enum建立简单的状态机
- JavaScript正则表达式与注册验证
- spring security3.x学习(3)_初探过滤器机制和auto-config用法
- delphi操作word
- abstract class和interface