Success Rate

CodeForces - 807C

给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a<=b。

(0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0)

解法:

(x+a)/(y+b)==p/q;
-->
x+a=np;
y+b=nq;
-->
a=np-x;
b=nq-y;
-->
二分n;
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define _ ios::sync_with_stdio(false),cin.tie(0) const int MAXN = 5010;
const int INF = 0xfffffff;
typedef long long ll; int t;
ll x,y,p,q; int main()
{
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;
}
 

CodeForce-807C Success Rate(二分数学)的更多相关文章

  1. Codeforces 807C - Success Rate(二分枚举)

    题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...

  2. Codeforces - 773A - Success Rate - 二分 - 简单数论

    https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<= ...

  3. codeforce C. Success Rate

    写完这道题目才发现自己对二分的理解太浅了 这题是典型的利用二分“假定一个问题可行并求最优解” 二分是通过不断缩小区间来缩小解的范围,最终得出解的算法 我们定义一个c(x) 表示判断函数 如果对任意y& ...

  4. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  5. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  6. 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 ...

  7. Codeforces Round #412 C. Success Rate

    C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. Codeforces 807 C. Success Rate

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

  9. C. Success Rate

    Success Rate 题目链接 题意 给你两个分数形式的数,然后有两种变化方式 上下都+1 仅下面部分+1 让你求第一个分数变化到第二个分数的最小步数. 思路 有几种特殊情况分类讨论一下. 首先我 ...

随机推荐

  1. Build a Beautiful oh-my-zsh Themes

    Selection Criteria double line; provide username, hostname, current directory; provide information o ...

  2. SpringMVC学习02(我的第一个SpringMVC程序)

    2.Hello,SpringMVC 2.1 配置版 1.新建一个Moudle , springmvc-02-hello , 添加web的支持! 2.确定导入了SpringMVC 的依赖! 3.配置we ...

  3. awk-02-内置变量

    内置变量 示例 1 FS和OFS 2 RS和ORS 3 NF是字段个数 4 NR和FNR NR 统计记录编号,每处理一行记录,编号就会+1,FNR 不同的是在统计第二个文件时会重新计数 NR和FNR区 ...

  4. Shell-03-表达式和运算符

    表达式和运算符 条件表达式语句 [ 1 -eq 1 ] [[ 1 -eq 1 ]] test 1 -eq 1 等同于 [ 1 -eq 1 ] [root@satest_192-168-3-121 sh ...

  5. unserialize反序列化 安鸾 Writeup

    关于php反序列化漏洞原理什么,可以看看前辈的文章: https://xz.aliyun.com/t/3674 https://chybeta.github.io/2017/06/17/浅谈php反序 ...

  6. WPF 中的 Command 命令

    <Window x:Class="CommandDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx ...

  7. 转:自增(自减)在Java与C中的区别

    转自:http://seiyatime.blog.sohu.com/84358295.html 话说昨日面试,在笔试的25个选择题中,涉及自增自减不止一两题,以前在开发过程中并没太在意这方面的问题,也 ...

  8. spring 》Cglib赋值

    第一个:字节码文件时带有ENHANCERBYCGLIB,FastClassByCGLIB组成的文件名 第二个:字节码文件时带有ENHANCERBYCGLIB 第三个:字节码文件时带有FastClass ...

  9. Win10安装gcc

    使用MinGW安装gcc 1.下载MinGW,地址 https://sourceforge.net/projects/mingw/files/ ,选择Download mingw-get-setup. ...

  10. validity属性返回对象中的属性值