写完这道题目才发现自己对二分的理解太浅了 这题是典型的利用二分“假定一个问题可行并求最优解”

二分是通过不断缩小区间来缩小解的范围,最终得出解的算法 我们定义一个c(x) 表示判断函数

如果对任意y>=x 当x满足条件的时候 y也满足条件 那么我们就一个不断缩小区间范围来确定最后的解

好扯了这么多犊子 来说下这道题目。。

我们的目的是对A,B 有 (x+A)/(y+B) == (p/q) 其中A<=B

那么可以转换为 A+x=k*p  B+y=k*q;

既 A=k*p-x, B=k*q-y;(A>=0,B>=0)

这里可以验证 对任意k >= x 当x满足条件的时候 k也满足条件(自己模拟一下就知道了)

ok 二分开搞

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
const int inf=1000000000;
int main()
{
int t;
cin>>t;
long long int x,y,p,q;
while(t--)
{
cin>>x>>y>>p>>q;
long long int temp=-1;
long long int l=1;
long long int r=inf;
while(l <= r)
{
long long int mid=(l+r)/2;
long long int A=mid*p-x;
long long int B=mid*q-y;
if(A>=0 && B>=0 && A<=B)
{
temp=mid;
r=mid-1;
}
else l=mid+1;
}
if(temp==-1) cout<<"-1"<<endl;
else cout<<temp*q-y<<endl;
}
return 0;
}

  

codeforce C. Success Rate的更多相关文章

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

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

  2. Codeforces Round #412 C. Success Rate

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

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

  4. Codeforces 807 C. Success Rate

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

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

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

  6. CodeForce-807C Success Rate(二分数学)

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

  7. C. Success Rate

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

  8. The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  9. Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

随机推荐

  1. python小技巧之把list组合成chain

    a=[] for i in range(10): a.append(i) for i,j in zip(a[:-1],a[1:]): print('%s=>%s'%(i,j)) 输出结果: 0= ...

  2. Reflexil

    https://github.com/sailro/Reflexil/issues/47 Instructions on how to install Reflexil would be much a ...

  3. [Mybatis]查询Sql得到一个字符串

    // find min date HashMap<String, String> minDateMap = new HashMap<String, String>(); min ...

  4. P3378 【模板】堆 (内含左偏树实现)

    P3378 [模板]堆 题解 其实就是一个小根堆啦,STL就可以解决,但是拥有闲情雅致的我学习了Jelly_Goat的左偏树,增加了代码长度,妙啊 Solution 1 STL STL 里面prior ...

  5. LNMP分离

    1.Nginx 找php 在nginx 配置文件的server里 添加 location ~.*\.(php|php5)?$ { root    /var/www/html/wwwcom; fastc ...

  6. jQuery常用AJAX-API

    目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取得服务端当前时间 简单形式:jQuery对象.load(url) 返回结果自动添加到jQuery对象代表的标签中间 如果是Servlet的话,采 ...

  7. grivaty,margin和padding的区别

    layout_margn是指组件距离父窗体的距离, padding是指组件中的内容距离组件边缘的距离 Layout_grivaty与grivaty的区别 layout_grivaty是指组件相对父窗体 ...

  8. Python Flask,cookie,session ,设置、获取、删除

    使用Response类的set_cookie()方法可以设置cookie: Response.set_cookie( key, //键 value='', //值 max_age=None, //秒为 ...

  9. Qt编写数据可视化大屏界面电子看板3-新建布局

    一.前言 能够新建布局,也是数据可视化大屏界面电子看板系统中的必备功能之一,新建布局这样的功能一般做到右键菜单中,单击新建布局菜单,弹出输入框要求输入新的布局的名称,为了更符合国情,直接支持中文名称, ...

  10. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(一) (转)

    http://www.cnblogs.com/liqingwen/p/6640861.html 一步步打造一个简单的 MVC 电商网站 - BooksStore(一) 本系列的 GitHub地址:ht ...