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.



——————————————————————————————————————
题目的意思是给出一个x,y,p,q要求(x+a)/(y+b)==p/q (a<=b),求b最小值
思路:y+b之后肯定是q的倍数,且倍数越大越可能,所以二分这个倍数加验证能否有可能实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long LL x,y,p,q; bool ok(LL n)
{
if(q*n-y>=p*n-x&&q*n>=y&&p*n>=x)
return 1;
return 0;
} int main()
{
int t; scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld%lld",&x,&y,&p,&q); LL l=1,r=1e9; LL ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(ok(mid))
{
r=mid-1;
ans=mid;
}
else
{
l=mid+1;
}
}
if(ans==-1)
printf("-1\n");
else
printf("%lld\n",ans*q-y);
}
return 0;
}

Codeforces807 C. Success Rate 2017-05-08 23:27 91人阅读 评论(0) 收藏的更多相关文章

  1. HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25993   Accepted: 12181 De ...

  2. 利用autotools工具制作从源代码安装的软件 分类: linux 2014-06-02 23:27 340人阅读 评论(0) 收藏

    编写程序(helloworld.c)并将其放到一个单独目录. helloworld.c: #include<stdio.h> int main() { printf("hello ...

  3. 随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏

    下面代码需要插入到MFC项目中运行,实现了计算机图形学中的L系统分形树. class Node { public: int x,y; double direction; Node(){} }; CSt ...

  4. Codeforces807 A. Is it rated? 2017-05-08 23:03 177人阅读 评论(0) 收藏

    A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. NYOJ-235 zb的生日 AC 分类: NYOJ 2013-12-30 23:10 183人阅读 评论(0) 收藏

    DFS算法: #include<stdio.h> #include<math.h> void find(int k,int w); int num[23]={0}; int m ...

  6. ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏

    IP Address Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose you are reading byte streams fr ...

  7. ZOJ3704 I am Nexus Master! 2017-04-06 23:36 56人阅读 评论(0) 收藏

    I am Nexus Master! Time Limit: 2 Seconds      Memory Limit: 65536 KB NexusHD.org is a popular PT (Pr ...

  8. 使用URLConnection获取网页信息的基本流程 分类: H1_ANDROID 2013-10-12 23:51 3646人阅读 评论(0) 收藏

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

  9. 认识C++中的临时对象temporary object 分类: C/C++ 2015-05-11 23:20 137人阅读 评论(0) 收藏

    C++中临时对象又称无名对象.临时对象主要出现在如下场景. 1.建立一个没有命名的非堆(non-heap)对象,也就是无名对象时,会产生临时对象. Integer inte= Integer(5); ...

随机推荐

  1. proxychains 安装

    一.安装下载源码: git clone https://github.com/rofl0r/proxychains-ng 编译和安装: cd proxychains-ng ./configure -- ...

  2. conductor 事件处理程序

    Introduction conductor中的事件提供工作流之间的松散耦合,并支持从外部系统生成和消耗事件. 包括: 1. 能够在外部系统像SQS或Conductor内部生成一个事件(消息). 2. ...

  3. Eclipse安装教程

    Eclipse安装教程     (Win7_64bit + Eclipse_64bit + JDK_8u131_64bit + python2.7.8 + PyDev5.7.0插件) 适用操作系统:W ...

  4. Mysql一些记忆

    mysql修改密码报错是yum 安装mysql5.7 是 出现无法登陆问题以及mysql error You must reset your password using ALTER USER sta ...

  5. linux下mongodb授权登录

    mongodb版本为3.2(目前最新),演示的是linux下的mongodb授权认证 第一次登录不启动授权(mongo默认不启动) ./mongod --dbpath=/home/db/data -- ...

  6. ContextLoaderListener和Spring MVC中的DispatcherServlet学习

    DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...

  7. Java的indexOf返回的是第一个匹配到的字符的索引位置,substring(a,b)获得字符串的一部分内容

    背景:我要实现一个功能,需要匹配两个字符串是否有相同的字符,所以就写了下面一个小方法,定义两个字符串a和b,循环遍历 b,如果a中有b的子串就将匹配数量num+1   遇到的问题:开始判断字符串中是否 ...

  8. SpringMVC上传文件的MultipartFile源码

    零.MultipartFile上传文件的具体实例如下: http://blog.csdn.net/swingpyzf/article/details/20230865 一.具体类和方法 上传文件主要方 ...

  9. Android网络类型判断(2g、3g、wifi)

    判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考   说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 ...

  10. 数据流中位数 · data stream median

    [抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值 ...