地址: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
  1. 4
    3 10 1 2
    7 14 3 8
    20 70 2 7
    5 6 1 1
output
  1. 4
    10
    0
    -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-;
  10. const double pi=acos(-1.0);
  11. const int K=1e6+;
  12. const int mod=1e9+;
  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=;i<=n;i++)
  24. {
  25. scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
  26. if(p==)
  27. {
  28. if(x!=)
  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)+)*q-y;
  51. else
  52. ans=ans-y;
  53. printf("%I64d\n",ans);
  54. }
  55.  
  56. }
  57. return ;
  58. }

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?的更多相关文章

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

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

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

  4. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

    E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x =  ...

  5. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  6. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)

    A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  7. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心

    C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...

  8. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题

    B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D. Volatile Kite

    地址:http://codeforces.com/contest/801/problem/D 题目: D. Volatile Kite time limit per test 2 seconds me ...

随机推荐

  1. 如何在ChemDraw中输入℃温度符号

    化学反应常常对于温度是有一定要求的,所以用ChemDraw化学绘图工具在绘制化学反应的时候常常会用到℃温度符号.但是一些才接触ChemDraw的用户朋友不知道怎么输入℃.针对这种情况本教程来给大家分享 ...

  2. echarts x轴坐标文字显示不全

    在echarts中应用柱状图或者折线图时,当数据量过多的时候,X轴的坐标就会显示不全(如下图图一),在ECharts图表组件内部有一个机制,用于统计xAxis坐标刻度的个数和图表宽度,从而会自动调整刻 ...

  3. jquery做简单特效

    1.点击触发消失效果 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  4. JZOJ.5306【NOIP2017模拟8.18】棋盘游戏

    Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...

  5. Javascript中的window.event.keyCode使用介绍

    <body onkeydown=" alert(window.event.keyCode)"> <body onkeydown="if(window.e ...

  6. codevs 5963 [SDOI2017]树点染色

     [题解]: #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...

  7. [黑金原创教程] FPGA那些事儿《设计篇 I》- 图像处理前夕

    简介 一本为入门图像处理的入门书,另外还教你徒手搭建平台(片上系统),内容请看目录. 注意 为了达到最好的实验的结果,请准备以下硬件. AX301开发板, OV7670摄像模块, VGA接口显示器, ...

  8. angular2+ 引用layDate日期选择插件

    layDate日期选择插件使用npm安装好像是行不通的,但angular2+的日期选择控件库又不能够支持时分秒的选择 在angular项目中引用layDate 1. 首先官网下载layDate独立版, ...

  9. FZU 2140 Forever 0.5(找规律,几何)

    Problem 2140 Forever 0.5 Accept: 371 Submit: 1307 Special Judge Time Limit: 1000 mSec Memory Limit : ...

  10. NoSQL 常用资源

    Hadoop:http://www.apache.org/dyn/closer.cgi/hadoop/common/ easyhadoop:https://github.com/xianglei/ea ...