Codeforces807 C. Success Rate 2017-05-08 23:27 91人阅读 评论(0) 收藏
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.
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
4
10
0
-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.
#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) 收藏的更多相关文章
- 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 ...
- 利用autotools工具制作从源代码安装的软件 分类: linux 2014-06-02 23:27 340人阅读 评论(0) 收藏
编写程序(helloworld.c)并将其放到一个单独目录. helloworld.c: #include<stdio.h> int main() { printf("hello ...
- 随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏
下面代码需要插入到MFC项目中运行,实现了计算机图形学中的L系统分形树. class Node { public: int x,y; double direction; Node(){} }; CSt ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 使用URLConnection获取网页信息的基本流程 分类: H1_ANDROID 2013-10-12 23:51 3646人阅读 评论(0) 收藏
参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...
- 认识C++中的临时对象temporary object 分类: C/C++ 2015-05-11 23:20 137人阅读 评论(0) 收藏
C++中临时对象又称无名对象.临时对象主要出现在如下场景. 1.建立一个没有命名的非堆(non-heap)对象,也就是无名对象时,会产生临时对象. Integer inte= Integer(5); ...
随机推荐
- IE下设置body{overflow:hidden;}失效Bug
问题重现: <p>There are no scrollbars on this page in sane browsers</p> html, body, p { margi ...
- Lock()与RLock()锁
资源总是有限的,程序运行如果对同一个对象进行操作,则有可能造成资源的争用,甚至导致死锁 也可能导致读写混乱 锁提供如下方法: 1.Lock.acquire([blocking]) 2.Lock.rel ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...
- webservice jaxws header验证
@WebService @HandlerChain public class UserService { ... } package com.xx.ws.header; import org.w3c. ...
- 第八章 高级搜索树 (a3)伸展树:算法实现
- Maven国内镜像-阿里云
国外的maven下载速度堪忧,大部分国内网络访问都很慢国内的阿里云同样提供了maven的文件镜像使用:1.在maven的setting.xml加入下段代码即可使用阿里云的maven镜像 <mir ...
- 数字组合 · Combination Sum
不能重复: [抄题]: 给出一个候选数字的set(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取. 例如,给出候选数组[2,3,6,7]和目标数字7,所求 ...
- MyEclipse2018.9.0设置全局编码
1.windows->Preferences打开"首选项"对话框,左侧导航,导航到general->Workspace 右侧Text file encoding,选择O ...
- Spring框架的事务管理的基本概念
1. 事务:指的是逻辑上一组操作,组成这个事务的各个执行单元,要么一起成功,要么一起失败! 2. 事务的特性 * 原子性 * 一致性 * 隔离性 * 持久性 3. 如果不考虑隔离性,引发安全性问题 * ...
- Golang之hello,beego
学习谢大神的beego记录 过程: 目录结构: 编译命令: go build -o myBeego.exe go_dev/day13/beego_example/main执行myBeego.exe即可 ...