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. Kong网关安装之Docker版(2)

    1.安装kong管理工具:konga或者kong-dashboard,这里选择konga 拉取konga镜像: sudo docker pull pantsel/konga:0.14.4 初始化kon ...

  2. RadioButton 自定义样式(带动画)

    <Style x:Key="Radbtn" TargetType="{x:Type RadioButton}"> <Setter Proper ...

  3. Apache网页优化与安全

    目录 一.Apache网页优化 1.1.概述 1.2.gzip介绍 1.3.Apache的压缩模块 二.网页压缩实验 2.1.检查是否安装mod_deflate模块 2.2.重新编译安装Apache添 ...

  4. 【笔记】主成分分析法PCA的原理及计算

    主成分分析法PCA的原理及计算 主成分分析法 主成分分析法(Principal Component Analysis),简称PCA,其是一种统计方法,是数据降维,简化数据集的一种常用的方法 它本身是一 ...

  5. SpringBoot开发一

    项目介绍 牛客高级项目课,主要是完成牛客网的讨论社区的搭建.项目在github上. 涉及到的技术架构: Spring,SpringBoot,SpringMVC,MyBatis,Redis,Kafka( ...

  6. ASP.NET Core 修改开源协议为MIT,.NET全平台 MIT协议开源了

    2021年7月23日,.NET开发团队完成了所有的.NET平台的相关框架的MIT协议更改,我们可以通过 https://github.com/dotnet/aspnetcore/issues/1887 ...

  7. windows下安装mysql5.6.47版本

    详情转载地址:https://www.cnblogs.com/alan-lin/p/9966917.html

  8. Java序列化bean保存到本地文件中

    File file = new File("D:\\softTemp\\student.out"); ObjectOutputStream objectOutputStream = ...

  9. easyexcel-导入

    package com.meeno.framework.util.easyexcel.entity; import cn.afterturn.easypoi.excel.annotation.Exce ...

  10. JSP页面添加当前时间

    JSP页面添加当前时间 一.时间格式化 1.引入标签 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/js ...