CF-807C
C. Success Ratetime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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?
InputThe 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.
OutputFor 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.
Exampleinput4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1output4
10
0
-1NoteIn 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+1,y+1或只y+1,使得x/y==p/q,问y最少要加几次,
若不能,输出-1.
由题意可得:
(x+a)/(y+b)=p/q
x+a=k*p , y+b=k*q
a=k*p-x , b=k*q-y
二分查找k,找到满足条件的最小值。
附AC代码:
#include<bits/stdc++.h>
using namespace std; const int INF=;
long long int x,y,p,q; int main(){
int t;
cin>>t;
while(t--){
cin>>x>>y>>p>>q;
long long int r=INF;
long long int l=;
long long int temp=-;
while(l<=r){
long long int mid=(l+r)/;
long long int a=mid*p-x;
long long int b=mid*q-y;
if(a>=&&b>=&&a<=b){
temp=b;
r=mid-;
}
else{
l=mid+;
}
}
if(temp==-)
cout<<"-1"<<endl;
else
cout<<temp<<endl;
}
return ;
}
CF-807C的更多相关文章
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
随机推荐
- 开始学习linux的一些疑问
Linux - Unix环境高级编程(第三版) 代码编译 https://www.linuxidc.com/Linux/2011-08/41228.htm ftp://ftp1.linuxidc.co ...
- vim 模式切换
1. 从插入模式退回到normal模式 <esc> <C-c> <C-[>
- 图像处理之滤波---滤波在游戏中的应用boxfilter
http://www.yxkfw.com/?p=7810 很有意思的全方位滤波应用 https://developer.nvidia.com/sites/default/files/akamai/ga ...
- Javascript MVC 学习笔记(二) 控制器和状态
今天进入第二个部分:控制器. 控制器和状态 从以往的开发经验来看.我们都是将状态保存在server的session或者本地cookie中,但Javascript应用往往被限制在单页面,所以我们也能够将 ...
- 九度OJ 1117:整数奇偶排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3174 解决:932 题目描述: 输入10个整数,彼此以空格分隔.重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到 ...
- Cocos2d-JS 项目接入 触控广告平台(基于anysdk2.0.2)
本文以Cocos2d-JS项目作为示例,讲解如何集成AnySDK-JS. 一.如何创建项目我就省略了,直接进入主题. 1.1 首先,我需要下载anysdk框架包,下载地址:http://www.any ...
- tornado之表单和模板
之前在indexHandler中通过self.write()方法在对应的网页中写入具体的字符信息. 如果我们想直接返回一个网页那么这个时候就需要用到模板了 首先在工程目录下新建一个template文件 ...
- Linux就该这么学--命令集合3(文本文件编辑命令)
1.cat命令查看纯文本文件(较短):(cat [选项] [文件]) cat -n showpath.sh 附录: -n 显示行号 -b 显示行号(不包括空行) -A 显示出“不可见”的符号,如空格, ...
- LVS的体系结构
LVS集群的体系结构 章文嵩 (wensong@linux-vs.org) 转自LVS官方资料 2002 年 4 月 本文主要介绍了LVS集群的体系结构.先给出LVS集群的通用体系结构,并讨论了其的设 ...
- [CPP - STL] functor刨根问底儿
作为STL六大组件之一,在STL源代码及其应用中,很多地方使用了仿函数(functor),尤其在关联型容器(如set.map)以及algorithm(如find_if.count_if等)中.虽然已经 ...