【题目链接】: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. 解决MyEclipse开启后总是不停的在Update index

    近期MyEclipse开启之后总是不停的在 update index,非常是耗时间. 查找资料发现Update index...是Maven在不断更新, 解决的方法例如以下: Window --> ...

  2. JeeCG团队招聘啦!

    JeeCG团队招聘啦! 小团队攻坚,创业氛围浓厚.盼望有激情的你增加. 岗位描写叙述: 1,负责Jeecg.jeewx 微信管理系统相关的研发工作.2,高度參与整个产品设计.參与微信相关项目开发.3. ...

  3. wpf Command canExecute 更新

    可以调用以下语句通知 CommandManager.InvalidateRequerySuggested();

  4. c14---排序,查找

    // // main.c // 冒泡排序 // // Created by xiaomage on 15/6/10. // Copyright (c) 2015年 xiaomage. All righ ...

  5. DDos攻击,使用深度学习中 栈式自编码的算法

    转自:http://www.airghc.top/2016/11/10/Dection-DDos/ 最近研究了一篇论文,关于检测DDos攻击,使用了深度学习中 栈式自编码的算法,现在简要介绍一下内容论 ...

  6. Node.js:Strea

    ylbtech-Node.js:Stream 1.返回顶部 1. Node.js Stream(流) Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...

  7. 什么是URL?网址的组成

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 和 Hypertext 以及 HTTP 一样,URL是Web中的一个核心概念.它是浏览器用来检索web上公布的任何资源的 ...

  8. exchange&nbsp;2010-备份还原

    操作系统:Windows Server 2008R2 \ Exchange2010 测试 1.使用Administraotr用户进行查看己有邮件,如下图: 2.备份Exchange2010整个数据库, ...

  9. Nginx 配置埋点js日志采集

    页面埋点&nginx日志采集 页面(web容器:httpd/nginx负载均衡 + apache server)<===> 日志采集服务器(nginx服务器) 通过某个页面跳转到我 ...

  10. Python基本数据类型之字符串str

    字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串 字符串的结构类型为'...' "..." "' ...