地址: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
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
output
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.

思路:略

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int p,x,y; int main(void)
{
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
int n;
LL x,y,p,q,x1,x2,ans;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
if(p==)
{
if(x!=)
printf("-1\n");
else
printf("0\n");
}
else if(p==q)
{
if(x!=y)
printf("-1\n");
else
printf("0\n");
}
else
{
x1=(q*x-y*p)/p;
if(x1*p<(q*x-y*p))
x1++;
x2=(y*p-x*q)/(q-p);
if(x2*(q-p)<(y*p-x*q))
x2++;
ans=max(x1,x2)+y;
if(ans%q)
ans=((ans/q)+)*q-y;
else
ans=ans-y;
printf("%I64d\n",ans);
} }
return ;
}

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. VS2008 对话框编辑器“即时预览”

    之前在VS2008中利用资源编辑器修改完对话框资源后,总是重新编译一下,然后Ctrl+F5运行来预览修改的效果,不断修改,不断编译,导致很费时,效率低下. 今天,发现了一个很好用的功能“Test Di ...

  2. Angular2 HttpClient (一)

    @angular/common/http 中的 HttpClient 类为 Angular 应用程序提供了一个简化的 API 来实现 HTTP 客户端功能.它基于浏览器提供的 XMLHttpReque ...

  3. 各种API中文文档下载地址

    转发: http://www.aseoe.com/api-download/download.html jquery easyui 帮助文档: http://download.csdn.net/dow ...

  4. Java知识点梳理——装箱和拆箱

    1.前言:Java是典型的面向对象编程语言,但其中有8种基本数据类型不支持面向对象编程,基本数据类型不具备对象的特性,没有属性和方法:Java为此8种基本数据类型设计了对应的类(包装类),使之相互转换 ...

  5. 通过chrome浏览器调试手机页面(IOS和Android)

    开发PC页面的时候使用chrome浏览器的开发者工具,可以很容易的捕获到页面的dom元素,并且可以修改样式,方便调试,但是手机上却很麻烦,因为手机上没有办法直接打开开发者工具查看元素.其实可以通过将设 ...

  6. 【NGUI】Unity3D UIGrid 排序、动态加载与销毁

    本帖最后由 Lucifer_H 于 2014-10-31 09:39 编辑 做 UIGrid 的动态加载是做游戏的装备列表时用到的,装备信息都是从后台发过来的,具体加载的代码如下: [C#] 纯文本查 ...

  7. webpack报错no postcss config...

    终端里运行的错误: 查了好多资料,最后找到解决办法,改为: const webpack = require('webpack'); // const autoprefixer = require('a ...

  8. 一个网络设备的常见功能--连通性检查SSRF漏洞--被黑客利用当做扫描器

    一.我们先来看一下很多网络设备都有的一个常见功能--连通性测试: 很多网络设备都具备与其他设备通信,联动的功能,例如网络设备联动安全设备,网络设备联动认证设备等等.此时都需要一个对端IP和对端端口号作 ...

  9. 模拟Push和Pop动作

    //利用CATransition来作模拟 //模拟Push - (void)pushView:(UIView *)pushView inView:(UIView *)inView { CATransi ...

  10. TabBar变透明

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];