【codeforces 807C】Success Rate
【题目链接】:http://codeforces.com/contest/807/problem/C
【题意】
给你4个数字
x y p q
要求让你求最小的非负整数b;
使得
(x+a)/(y+b)==p/q
同时a为一个整数且0<=a<=b
【题解】
/*
(x+a)/(y+b)==p/q;
则
x+a=np
y+b=nq
(以上结论只在p和q是互质的情况下有效,当然题目有说p和q互质)
a=np-x
b=nq-y
a<=b
因为p<q所以n越大b会越大;
又p和q都大于等于0,所以n越大,a和b都是不下降的
二分n
*/
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
int t;
LL x,y,p,q;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> t;
while (t--)
{
cin >> x >> y >> p >> q;
LL l = 0,r = 1e9,ans = -1;
while (l<=r)
{
LL mid = (l+r)>>1;
LL a=mid*p-x,b=mid*q-y;
if (a>=0 && b>=0 && a<=b)
{
ans = mid;
r = mid-1;
}
else
l = mid+1;
}
if (ans==-1)
cout <<-1<<endl;
else
cout <<ans*q-y<<endl;
}
return 0;
}
【codeforces 807C】Success Rate的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【74.89%】【codeforces 551A】GukiZ and Contest
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 807 C. Success Rate
http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- CentOS 7 安装Nginx做反向代理
题记 须要使用nginx的反向代理功能,測试环境为centos+NGINX 1.8.0. 跳过一些繁琐的问题,直接记录核心 步骤 (1)centos 安装在VM中.因此须要注意网络连接问题 (2)安装 ...
- Online Object Tracking: A Benchmark 论文笔记
Factors that affect the performance of a tracing algorithm 1 Illumination variation 2 Occlusion 3 Ba ...
- Thinking in Java:容器深入研究
1.虚线框表示Abstract类,图中大量的类的名字都是以Abstract开头的,它们仅仅是部分实现了特定接口的工具,因此创建时能够选择从Abstract继承. Collections中的实用方法:挑 ...
- 2016.04.22,英语,《Vocabulary Builder》Unit 17
anim, comes from the Latin anima, meaning 'breath' or 'soul'. animism: ['ænɪmɪzəm] n. 泛灵论,精神存在论,神创宇宙 ...
- hdoj--1045--Fire Net(二分图)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj--2015--偶数求和(水题)
偶数求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- linux下创建公钥
# linux下创建公钥 链接地址:https://www.cnblogs.com/ibyte/p/6086630.html 示例: scp -r /home/yutang/.ssh/id_rsa.p ...
- 洛谷P2181 对角线(组合数)
题目描述 对于一个N个定点的凸多边形,他的任何三条对角线都不会交于一点.请求楚图形中对角线交点的个数. 例如,6边形: 输入输出格式 输入格式: 第一行一个n,代表边数. 输出格式: 第一行输出交点数 ...
- String[]转化暴露“思维误区”
那天写code,用到这个,强转,将页面传来的值转换为数组,结果是,当页面传来的只有一个值时,它是无法转换为数组的,只能获得1个string,只有length>1时才会转化为数组的形式,报的错误是 ...
- Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版)
Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版) 前提概要 项目上需求要适应Oracle数据库,当然这和某EF框架也有关. 因为Oracle 的表名和列 ...