【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【74.89%】【codeforces 551A】GukiZ and Contest

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Codeforces 807 C. Success Rate

    http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. linux高级技巧:rsync同步(二)

    1.配置两个节点同步         上个帖子已经展示了同步一个主机的方法.这次我们再加入一个要同步的主机. 方法上并无二质.         1.首先显示我们的控制台:                ...

  2. chromium添加flash

    http://blog.csdn.net/lainegates/article/details/27830333

  3. ubuntu系统源的更新

    以前没有注意,ubuntu的系统的源原来每次在GUI操作后都会追加在后面而不是覆盖,比如先添加了上海交大的源那么后来如果发现很慢下载不了,而这个包已经在apt-cache里面了,下次如果要instal ...

  4. Python的filter与map内置函数

    简单的记录下这两个函数的功能: list(filter(lambda x : x % 2, range(10))) 上例是返回了0-10之间的所有基数组成的列表.filter()有2个参数,第一个参数 ...

  5. POJ 1703 带权并查集

    直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...

  6. java题(转载)

    1.下面中哪两个可以在A的子类中使用:( ) class A { protected int method1 (int a, int b) { return 0; } } A. public int ...

  7. JavaScript实现鼠标效果

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  8. angular实现动态的留言板案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. vue-cli 2.0 常用命令

    一.查询npm版本 npm -v 二.安装npm npm install npm g 三.安装webpack npm install webpack -g 四.安装vue命令行工具 npm insta ...

  10. CaptCha的现状与未来

    2011年的老文.................转自于伯乐在线:http://blog.jobbole.com/4655/       有一个机会,朋友推荐一个创业的哥们给我认识,方向就是验证码识别 ...