地址:http://codeforces.com/contest/807/problem/C

题目:

C. Success Rate
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

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

思路:略

 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?相关推荐

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

  2. 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], ...

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

  4. VK Cup 2017 - Round 1

    传送门 A. Bear and Friendship Condition(思维or完全图判定) •题意 给你n个人,m个朋友关系 朋友是会传递的,若A B是朋友,A C是朋友,则必须有B C的朋友关系 ...

  5. VK Cup 2017 - Round 2

    FallDream打的AB都FFT了,只剩一个我打的C,没进前一百,之后看看马拉松复活赛有没机会呗. A. Voltage Keepsake 题目大意:n个东西,每个东西一开始有bi能源,每秒消耗ai ...

  6. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)

    这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...

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

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

  9. 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 >中某个数的公约数.而 ...

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

最新文章

  1. Linux+Qt 下同一数据空间vfork多进程间通信的一种高效便捷方式(信号槽直接调用)
  2. 当不使用会话状态时禁用它
  3. oracle中将number类型毫秒值转为时间类型
  4. IIS下的FTP使用
  5. linux同步IO: sync、fsync与fdatasync
  6. 160 - 39 damn
  7. audio unity 加速_浅谈Unity中Android、iOS音频延迟
  8. pythonclass实例化_Python中实例化class的执行顺序示例详解
  9. 程序员的必杀技——编程全能词典
  10. 20.20 告警系统主脚本;20.21 告警系统配置文件;20.22 告警系统监控项目
  11. element-plus Table 表格自定义列模板使用及问题
  12. Keil自动格式化代码
  13. 状态反馈控制与状态观测器设置以及利用LQR方法求取状态反馈矩阵
  14. 三菱Fx系列PLC的编程口协议
  15. 这个英文语法校正插件很赞!可以校正邮件,也可以校正论文
  16. 冲激函数与卷积的多次邂逅
  17. Everything+cpolar搭建在线资料库,实现随时随地访问
  18. missing iso 9660 image the installer has tried to mount image #1
  19. python+itk+读取dicom数据,并保存为nii文件
  20. 下载vue模板框架并使用

热门文章

  1. 苹果Mac应用音频捕获录制工具:​​​​​​​​TunesKit Audio Capture
  2. 如何在 macOS Monterey 中管理应用程序和联系人的 Mac 通知?
  3. MWeb Pro for Mac(静态博客生成软件)
  4. 运维监控三剑客之Zabbix
  5. git本地分支和远程分支改名
  6. 使用enum建立简单的状态机
  7. JavaScript正则表达式与注册验证
  8. spring security3.x学习(3)_初探过滤器机制和auto-config用法
  9. delphi操作word
  10. abstract class和interface