Codeforces - 773A - Success Rate - 二分

https://codeforces.com/problemset/problem/773/A

一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<=p/q,错在这些数是离散的,不能由两边异号判定一定存在这个交点。

然后改成枚举d,使得y=d*q,这样就一定是倍数了。然后就是要想清楚了,找不到这样卡在中间的d,其实都是因为d不够大的原因,d够大保证是可以的除非正确率是100%。

然后就是二分的上界,按道理q的最大值是1e9,y的最大值也是1e9,他们的公倍数肯定在1e18范围内(p和q任意组合能得到的比值最多就是1e18/2,把1e18都枚举完肯定能遍历所有能构造出的情况),那么最大值肯定是1e18/q,多个1都不行。

二分,老朋友了,while(1),当l==m的时候是边界,特判就好。

#include<bits/stdc++.h>
using namespace std;
#define ll long long

string a,b;
string c;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        ll x,y,p,q;
        scanf("%lld%lld%lld%lld",&x,&y,&p,&q);
        ll l=1,r=1e18/q,m;
        ll ans;
        while(1){
            m=(l+r)>>1;
            //cout<<l<<" " <<r<<" "<<m<<endl;

            if(l==m){
                ll del=l*q-y;
                if(del>=0&&(x+del)>=l*p&&x<=l*p){
                    ans=l;
                }
                else if((r*q-y)>=0&&(x+(r*q-y))>=r*p&&x<=r*p){
                    ans=r;
                }
                else{
                    ans=-1;
                }
                break;
            }
            if(m*q<y){
                l=m+1;
                continue;
            }
            ll del=m*q-y;
            if((x+del)>=m*p&&x<=m*p){
                r=m;
                //cout<<m<<" ok"<<endl;
            }
            else{
                l=m+1;
            }
        }

        printf("%lld\n",ans==-1?-1:ans*q-y);

    }
}

原文地址:https://www.cnblogs.com/Yinku/p/10414473.html

时间: 2024-10-11 04:07:02

Codeforces - 773A - Success Rate - 二分的相关文章

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

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 <cs

Codeforces 807C - Success Rate

题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再提交几次可以达到目标正确率p/q; 解题思路:假设提交B次,正确A次,那么可以得到(x+A)/(y+B)=p/q,可以推出x+A=k*p,y+B=k*q.那么A=k*p-A,B=K*q-A; 这样我们只需要二分枚举k,判断A,B是否满足(0<=A<=B)即可. 1 #include<iost

Codeforces Round #412 C. Success Rate

Success Rate You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x?/?y. Your f

AC日记——Success Rate codeforces 807c

Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces gym 100517(二分,同方向判断)

题意:给了一个凸包,按顺时针顺序给点,点数不超过10万,再给了两个不同点,点严格在凸包内,凸包保证没有三点共线,问凸包上有多少对点(pi, pj),满足pi和pj的线段 与 两个点的线段严格相交,线段间严格相交意思是交点不在端点. 链接:http://codeforces.com/gym/100517 (K题) 解法:设凸包有n个点,将凸包点集p扩大一倍,变为2n个点.枚举前n个点,每次枚举到 i ,在[i+1, i+n-1]内进行二分,找到两个点p1,p2,满足p1和p2是"最靠近"

Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

Digital collectible card games have become very popular recently. So Vova decided to try one of these. Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck

CodeForces 670D1 暴力或二分

今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find