Success Rate CodeForces - 807C (数学+二分)
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 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.
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
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
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.
题目链接:
题意:给你4个整数,x,y,p,q,P/Q的范围是[0,1],让你求最小的提交数量b,其中a个提交成功使 ( x + a ) / ( y + b ) == p / q
我们设一个系数n,使
p*n=x+a
q*n=y+b
那么,
a=p*n-x
b=q*n-y
根据题意,我们知道a和b满足的条件为b>=a>=0
并且观察可知a和n呈正相关,那么我们要求最小的a,可以通过二分n来得到
根据题目的数据范围,n的二分区间为0~1e9
注意下-1的情况就行了。
细节看我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
ll x,y,a,b;
int main()
{
gbtb;
cin>>t;
while(t--)
{
cin>>x>>y>>a>>b;
ll l=;
ll r=1e9;
ll mid;
ll ans=-;
while(l<=r)
{
mid=(l+r)>>;
ll a1=a*mid-x;
ll a2=b*mid-y;
if(a1>=&&a2>=&&(a1<=a2))
{
ans=mid;
r=mid-;
}else
{
l=mid+;
}
}
if(ans==-)
{
cout<<-<<endl;
}else
{
cout<<b*ans-y<<endl;
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Success Rate CodeForces - 807C (数学+二分)的更多相关文章
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- CodeForce-807C Success Rate(二分数学)
Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a< ...
- Codeforces Round #412 C. Success Rate
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 807 C. Success Rate
http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...
- Codeforces807 C. Success Rate 2017-05-08 23:27 91人阅读 评论(0) 收藏
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- C. Success Rate
Success Rate 题目链接 题意 给你两个分数形式的数,然后有两种变化方式 上下都+1 仅下面部分+1 让你求第一个分数变化到第二个分数的最小步数. 思路 有几种特殊情况分类讨论一下. 首先我 ...
- Codeforces 807C - Success Rate(二分枚举)
题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...
- codeforces 807 C. Success Rate(二分)
题目链接:http://codeforces.com/contest/807/problem/C 题意:记 AC 率为当前 AC 提交的数量 x / 总提交量 y .已知最喜欢的 AC 率为 p/q ...
随机推荐
- 三、Tableau筛选器的使用
一.使用筛选器制作联动效果 注意,下面两幅图以‘是否盈利’来添加颜色标签 图一:地图 图二:月度销售客户细分 图三:月度销售产品类别 1.新增仪表盘:将图一.图二.图三拉进同一个仪表盘 2.在图一原图 ...
- 用beamoff给VMware的Mac OS X 10.10.x加速
前言 今天刚在VMware里装了个Yosemite,然后测试了下看电影,真j8卡,试了下在vm里打开3d加速,然并卵,直接显示不能打开3d加速,然后找了下发现有个vga的什么软件,是vmware里的显 ...
- Unittest框架小结
在日常的自动化测试过程中,Python里有一个自带的单元测试框架是unittest模块,简单易用,这里简单介绍下其主要的用法. Unittest测试框架主要包含四个部分 TestCase 也就是测试用 ...
- WPF模板(一)详细介绍
本次随笔来源于电子书,人家的讲解很好,我就不画蛇添足了. 图形用户界面应用程序较之控制台界面应用程序最大的好处就是界面友好.数据显示直观.CUI程序中数据只能以文本的形式线性显示,GUI程序则允许数据 ...
- centos7+nginx负载均衡Tomcat服务
接着上一篇:www.cnblogs.com/lkun/p/8252815.html 我们在上一篇在一台centos7服务器上部署了两个nginx,接下来我们使用一个nginx实现tomcat的负载均衡 ...
- 强化学习(一)—— 基本概念及马尔科夫决策过程(MDP)
1.策略与环境模型 强化学习是继监督学习和无监督学习之后的第三种机器学习方法.强化学习的整个过程如下图所示: 具体的过程可以分解为三个步骤: 1)根据当前的状态 $s_t$ 选择要执行的动作 $ a_ ...
- 转://Oracle 数据备份与恢复微实践
1.模拟控制文件丢失后的数据库恢复(完全恢复) 今天的主题是备份与恢复,目的就是保护数据的安全性,众所周知Oracle之所以在市场上占据了50%的份额,与它提供了强大的数据保护措施是分不开的,下面我们 ...
- 【转】APK反编译
学习和开发Android应用有一段时间了,今天写一篇博客总结一下Android的apk文件反编译.我们知道,Android应用开发完成之后,我们最终都会将应用打包成一个apk文件,然后让用户通过手机或 ...
- (1)ESP8266微信门铃
http://rayuu.com/2017/11/13/esp8266-wechat-doorbell/(留做参考) 就是当门铃按键按下,微信会收到消息提醒. 若在家就算了,没在家会受到远程提示. 自 ...
- flask上传excel文件,无须存储,直接读取内容
运行环境python3.6 import xlrd from flask import Flask, request app = Flask(__name__) @app.route("/& ...