codeforces807div2 C.Success Rate[二分][数学]

JS逆向实战:从宿务航空机票搜索到参数签名算法解析 在现代Web应用开发中,前端与后端的数据交互安全是核心技术环节。其原理通常涉及参数签名、令牌验证等机制,用于防止未授权访问和数据篡改。这些技术的核心价值在于保障接口调用的合法性与数据完整性,广泛应用于电商、金融、旅行预订等对数据安全要求较高的场景。以机票搜索为例,网站常通过动态生成的签名参数来验证请求来源。本文以宿务太平洋航空为案例,深入解析其搜索请求中关键参数(如signature)的生成逻辑,并演示如何通过浏览器开发者工具进行断点调试、变量追踪,最终使用Node.js与crypto-js库本地复现完整请 阅读详情

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.


题意: 现在的正确率是 a/b , 要达到c/d ,可以进行任意次的正确与非正确提交,求最小的提交次数能够得到c/d, 如果不能得到 pirnt -1

思路:

假设要进行p次正确提交,q-p次非正确提交, 则 (a+p)/(b+q) == c/d

由题意知c、d互质,所以 (a+p)/(b+q)应该是 c/d的非最简分数或最简分数, 即 (a+p)/(b+q) == nc/nd , 其中  0<=p<=q  n>=1  且都是整数

所以 p = nc-a,    q = nd-b     p、q都是n的递增函数, 所以二分n寻找最小的n满足条件,无满足输出-1

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        LL l = 1, r = 1e9;
        LL ans = -1;
        while(l<=r)
        {
            LL mid = (l+r) >> 1;
            //cout << mid << endl;
            LL p = mid*c-a, q = mid*d-b;
            if(p>=0 && q>=0 && p<=q)
            {
                r = mid-1;
                ans = q;
            }
            else
                l = mid+1;
        }
        cout << ans << endl;
    }
    return 0;
}


MOS五类漏电流与优化策略 分析了五种主要漏电流机制:1)反偏结泄漏,由PN结热激发和隧 阅读详情

相关推荐

【G4基础03】2.3 How to Specify Materials in the Detector

定义探测器材料

adelall的博客 489

Codeforces Round #412 C. 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

DorMOUSENone的博客 1502

DQN网络代码调用env.reset()后存储观测信息报错问题ValueError: setting an array element with a sequence.解决处理

**reset()函数返回的是一个array类型以及其中数据的type!模拟游戏进行学习任务。因此需要将observation指定为真正需要的array信息即可,得到的的变量observation类型与Agent存储时的。因此怀疑返回的observation有问题,于是通过。报错信息提示为数据维度不对应,也即最初通过。在强化学习DQN网络代码实现过程中,针对。

Vulcan_Q的博客 4092

codeforces round # 412 c(数论)

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

small__snail__5的博客 454

Python中优化模块scipy.optimize的优化算法介绍

如果你有一个特殊的优化问题,你可以使用对象来自定义问题的求解。# 自定义目标函数# 自定义优化问题custom_problem.fun = custom_objective_function # 目标函数custom_problem.x = [1, 1] # 初始猜测# 执行优化# 打印结果在上述例子中,我们自定义了一个对象,并指定了目标函数和初始猜测,然后使用minimize函数进行求解。Scipy的优化模块提供了丰富的功能,适用于各种优化问题。

Pandas120的博客 3045

【数据同化第一期】基本概念介绍

【数据同化第一期】基本概念介绍

WW、forever的博客 1901

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

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 fou

mengxiang000000 1064

基于大模型与知识图谱的对话引导意图澄清系统技术解析

本文提出了一种基于知识图谱与大语言模型的意图澄清系统,旨在解决传统RAG模型在跨文档推理和主动澄清方面的不足。系统通过知识图谱驱动的多跳推理机制和链式思维引导的对话流程,实现了动态信息补全和意图澄清。核心创新包括:1)知识图谱构建与节点选择算法;2)标签探索策略与原子事实提取方法;3)主动澄清引擎的工作流程。实验表明,该系统能有效提升复杂查询的准确率,在医疗、金融等领域实现精准交互。关键代码展示了节点选择、标签匹配和事实提取等核心组件的实现细节。

410

Codeforces Round #412 ( Div. 2) C - Success Rate(二分)

C. Success Rate time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are an experienced Codeforces user. Today you found out that during yo

妍恐龙的博客....^~^眼熟我 575

分享一个我自用的 Python 消息发送模块,支持邮件、钉钉、企业微信

🔥 分享一个我自用的 Python 消息发送模块,支持邮件、钉钉、企业微信

XiaoqiangClub的博客 476

【GameFramework框架内置模块】8、文件系统(File System)

最近好忙,鸽了好久。不能颓废了,继续发力。今天分享的是GF框架的文件系统。首先,引用比较官方的说法:GF的FileSystem虚拟文件系统,使用类似磁盘存储的感念对零散的文件进行集中管理,优化资源加载时产生的内存分配,也可以对资源进行局部片段加载,极大的提升了资源加载的性能。通俗点讲就是,我这个模块就是加载磁盘文件的,优化加载时产生的内存分配。那么,它是怎么优化的呢?

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 2449

codeforces807div2 D.Dynamic Problem Scoring[暴力][贪心]

D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya and Petya take part in a C

Qer的博客 581

图像处理:张量分解在图像处理领域的应用

随着计算机视觉和图像处理技术的快速发展,图像数据呈现出高维、多模态、海量等特点。传统的矩阵分解方法已经无法有效处理这些复杂的图像数据。张量分解作为一种高阶矩阵分解方法,可以很好地捕捉图像数据的高维结构信息,在图像处理领域展现出巨大的应用前景。本节介绍几种常用的张量分解算法,包括高阶SVD(HOSVD)、Tucker-ALS、CP-ALS等。这些算法的核心思想是通过迭代优化的方式,交替更新张量分解的因子矩阵,直到收敛为止。综上所述,本文系统地介绍了张量分解技术在图像处理领域的应用。

AI天才研究院 7102

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) C. Success Rate 数学

链接: http://codeforces.com/contest/807/problem/C 题意: 给你x,y,p,q,现在让x和y分别加上一个数,使得(x+dx)/(y+dy)==p/q。其中dy&gt;=dx&gt;=0,要求加的数最小 题解: 首先需要判断特殊情况,当p=q时,如果x=y,那么就直接输出0,否则输出-1 当p=0时,如果x=0,那么就直接输出0,否则...

花海 212

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

CodeForces807c_二分+思维

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

no2015214099的博客 389

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

codeforces 807C Success Rate 数论

C. Success Rate time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are an experienced Codeforces user. Today you found out that during yo

黑码 778

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Codeforces 807C(二分)

问题描述: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 ra...

wust_cyl的博客 402

HDU 6059 Kanade's trio (字典树, 2017 Multi-Univ Training Contest 3)

Problem含 N 个数字的 A 数组,求有多少个三元组 (i,j,k) 满足 i<j<ki\lt j\lt k 且 (Ai⊕Aj)<(Aj⊕Ak)(A_i \oplus A_j) < (A_j\oplus A_k)Limit1≤T≤201\le T \le 201≤∑n≤5×1051\le \sum n\le 5\times 10^50≤Ai≤2300\le A_i\le 2^{30}Idea利

DorMOUSENone的博客 2333

C. Success Rate (codeforces)

今天开始补cf的题。遇到二分的题目, 补了这道题才发现自己对二分的理解还是太浅显了。 题目如下: C. Success Rate time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are an experienced Cod

落风听雨 570

# CF #807 Div.2(A - D)

​给定2n个数和k,请问是否存在n对(i,j)abs(ai-aj)>=k​先排序然后对1和n+1,2和n+2连续比对。​赛时拖了节奏因为看成了==k下次注意读题。

DM010maker的博客 395

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) C

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

Joky_2002的博客 420

Codeforces Round #412 (Div. 2)(Codeforces 807 ABCDE)

Codeforces Round #412 (Div. 2, base on VK Cup 2017 Round 3)比赛时过了3题,rating暴涨90+。。一定是上次只做出一题拉低了rating。。这场感觉AB都是题面杀。。然后比赛时看了DE感觉都可做,赛后补了一下。 官方题解依旧是高质量,耐心读,即使是会的题,也绝对收获不少。样例代码也很棒。 官方题解传送门:%touristCodefor

P.H.S 757

[刷题]Codeforces Round #412(Div. 2) - C. Success Rate

PROBLEMSET里面神tm搜不到这题,很迷。所以标题就只好注明比赛出处而没法标明题号了。DescriptionYou are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x

蟹脑板的博客 579

局部搜索算法-成功失败法

成功失败方法 成功失败法用于在局部搜索中确定搜索的区间同时也可以求解最小值,首先确定一个函数的搜索区间,然后不断缩短区间,直到搜索区间逼近最小值。 成功失败法根据每次选择的两个区间端点的函数值大小,判断搜索的“成功”与“失败”,然后进行跨步、反向、小步搜索。 成功失败法主要用于单峰函数的搜索。 成功失败法(求最小值)步骤: 用于确定搜索区间: (1)函数定义域为RnR^n首先确定一个初

Qer的博客 3053

Codeforces Round #807 (Div. 2) A-C题解

CF Round 807 Div2 题解 Codeforces Round #807 Div 2. Editorial

GoWilli的优快云 469

除法取模逆元,扩展欧几里得,费马小定理[数学]

一、除法取模逆元 在算法设计中,常会遇到求 a/b mod m的计算,当a很大,或者b很大,使得a/b的值无法直接计算的时候,通常采用逆元的方法,化除法为乘法。(逆元的概念在离散数学中 有学习) a/b mod m 等价计算为 a*k mod m (k是b的模m乘法逆元) 证明过程: 由于k是b的模m乘法逆元。 即 b*k mod m == 1 b*k = xm + 1 k = (xm

Qer的博客 4317
上一篇: 背包dp模板
下一篇: codeforces807div2 D.Dynamic Problem Scoring[暴力][贪心]
Qer_
博客等级 码龄11年 34粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值