Codeforces 807C Success Rate【二分+数学思维】

本文探讨了如何通过控制新增提交的数量及成功与否来调整Codeforces平台上的个人成功率至目标比率p/q的问题。文中提出了一种有效的算法策略,通过二分查找确定最小的提交次数,以实现目标成功率。

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

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 favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q?

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.

Each of the next t lines contains four integers x, y, p and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0).

It is guaranteed that p / q is an irreducible fraction.

Hacks. For hacks, an additional constraint of t ≤ 5 must be met.

Output

For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve.

Example
Input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
Output
4
10
0
-1
Note

In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.

In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.

In the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7.

In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1.


题目大意:

现在你提交的正确率是x/y目标达成的正确率是p/q

现在你可以无限次提交,同时可以控制一道题的正确与否,问最少提交多少次可以达到目标正确率。


思路:


问题转换变成:

找到最小的一个值B使得:(x+A)/(y+B)==p/q.这里要保证(A<=B);

那么此时已知:

x+A=k*p

y+B=k*q.

那么A=k*p-x,B=k*q-y..

很显然,当k越小的时候,A越小,所以这里二分k值。

然后判断B是否小于等于A即可。

同时要保证A,B>=0.


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll x,y,p,q;
        scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
        ll ans=-1;
        ll l=0;
        ll r=1000000000;
        while(r-l>=0)
        {
            ll mid=(l+r)/2;
            ll A=mid*p-x;
            ll B=mid*q-y;
            if(A>=0&&B>=0&&A<=B)
            {
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        if(ans==-1)printf("-1\n");
        else 
        printf("%I64d\n",ans*q-y);
    }
}


当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值