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)优 ...
随机推荐
- SDUT 1068-Number Steps(数学:直线)
Number Steps Time Limit: 1000ms Memory limit: 10000K 有疑问?点这里^_^ 题目描写叙述 Starting from point (0,0) ...
- Unsupported major.minor version (jdk版本错误)解决方案 办法
如果你遇到了 Unsupported major.minor version ,请认真看一下,说不定会有帮助. 我花两个小时总结的经验,你可能10分钟就得到了. ^**^ 一.错误现象: 当改变了jd ...
- Introducing Gradle (Ep 2, Android Studio)
https://www.youtube.com/watch?v=cD7NPxuuXYY Introducing Gradle (Ep 2, Android Studio) https://www ...
- win10下rose2003安装与破解(图解)
系统刷成了win10的,因为选择的是全新安装的方式,所以开发工具又得又一次安装了,rose尽管好用.但是安装破解还是有点麻烦,这里整理一下.备用,下回就不须要去网上搜索了. 安装文件下载地址:链接: ...
- Day20 Java Socket使用
Java中Socket的使用 client端 package org.tizen.test; import java.io.IOException; import java.io.OutputStre ...
- Mysql 存储过程使用游标
-- 完整例子 CREATE PROCEDURE test BEGIN -- 定义参数 DECLARE _id INT; -- 定义游标 DECLARE no_more_products ...
- 个人开发者帐号--我是如何实现在另一台mac上真机调试的
本文转载至 : http://blog.csdn.net/chenyong05314/article/details/8689534 注:本人有一台mac电脑,之前申请开发者帐号的时候直接就是在这 ...
- 在苹果iOS平台中获取当前程序进程的进程名等信息
本文由EasyDarwin开源团队成员Penggy供稿: Objective-C 提供 NSProcessInfo 这个类来获取当前 APP 进程信息, 然而我们的静态库是 pure C++ 工程. ...
- 九度OJ 1122:吃糖果 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...
- Geoffrey E. Hinton
https://www.cs.toronto.edu/~hinton/ Geoffrey E. Hinton I am an Engineering Fellow at Google where I ...