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 ysubmissions, 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 xyp 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
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 to9 / 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+a)/(y+b) = p/q 来表示其核心思想,其中a 为做对的题目,b为做的题目,则有x+a = k*p,y+b = k*q.且有0<=a<=b,两式合并可得k>=x/p,k>=(y-x)/(q-p), 则如要满足两个等式,k应取其中较大的值(注意:若取得的k为小数,我们需要的是最小整数解,加1),最后注意一下特殊情况 p = 0,p = q.

实现代码:

#include <bits/stdc++.h>
using namespace std; int main() {
long long n, x, y, p, q, k;
cin >> n;
for (int i = ; i < n; i++) {
cin >> x >> y >> p >> q;
if (p == ) {
if (x == ) cout << << endl;
else cout << - << endl;
}
else if (p == q) {
if (x == y) cout << << endl;
else cout << - << endl;
}
else {
long long k1 = (y-x)/(q-p) + (((y-x)%(q-p)) ? : );
long long k2 = x/p + ((x%p) ? : );
k = max(k1,k2);
cout << k*q-y << endl;
}
}
return ;
}

Codeforces Round #412 C. Success Rate的更多相关文章

  1. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

    A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  2. Codeforces Round#412 Div.2

    A. Is it rated? 题面 Is it rated? Here it is. The Ultimate Question of Competitive Programming, Codefo ...

  3. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心

    A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #412 Div. 2 第一场翻水水

    大半夜呆在机房做题,我只感觉智商严重下降,今天我脑子可能不太正常 A. Is it rated? time limit per test 2 seconds memory limit per test ...

  5. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?

    地址:http://codeforces.com/contest/807/problem/C 题目: C. Success Rate time limit per test 2 seconds mem ...

  6. Codeforces Round #412 A Is it rated ?

    A. Is it rated? time limit per test  2 seconds memory limit per test  256 megabytes Is it rated? Her ...

  7. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring

    地址:http://codeforces.com/contest/807/problem/D 题目: D. Dynamic Problem Scoring time limit per test 2 ...

  8. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. Codeforces Round #412 B. T-Shirt Hunt

    B. T-Shirt Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 10-(基础入门篇)MQTT介绍

    https://www.cnblogs.com/yangfengwu/p/9953920.html 看到这个项目第一想法肯定需要一个服务器,所有的wifi设备和手机都去连接这个服务器,然后服务器进行信 ...

  2. CF1105E Helping Hiasat 最大团

    传送门 发现自己不会求最大团了可海星 如果将每一个朋友看做点,将两个\(1\)之间存在\(2\)操作的所有朋友之间互相连边,那么我们最后要求的就是这个图的最大独立集. 某个图的最大独立集就是反图的最大 ...

  3. 一头雾水的"Follow The Pointer"

    原文:一头雾水的"Follow The Pointer" 一头雾水的"Follow The Pointer"                           ...

  4. 基于uFUN开发板的心率计(一)DMA方式获取传感器数据

    前言 从3月8号收到板子,到今天算起来,uFUN到手也有两周的时间了,最近利用下班后的时间,做了个心率计,从单片机程序到上位机开发,到现在为止完成的差不多了,实现很简单,uFUN开发板外加一个Puls ...

  5. hexo——轻量、简易、高逼格的博客

    背景 写blog虽然经历了N多不同时代的产品,恒久不变的始终是自己无人问津的网站.虽然没几个人看,还是隔断时间就要折腾一下.从最开始的wordpress,到tale,到现在的hexo,网站变得越来越简 ...

  6. linux journalctl 命令

    目录 Help 输出所有的日志记录 匹配(match) 把日志保存到文件中 限定日志所能占用的最高容量 查看某次启动后的日志 查看指定时间段的日志 同时应用 match 和时间过滤条件 按 unit ...

  7. Webpack 2 视频教程

    这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲解的. 这个基本就是目前 ...

  8. 开发CMDB系统

    背景: 在现网环境中服务器多了每天服务器的配置 情况我们很难记住,当某台服务器硬件配置变化后可以第一时间了解,某台服务器出现问题时可以快速定位机架位置,之前都是excel文档,要查某项数据时极不方便. ...

  9. 【课程总结】Linux内核分析课程总结

    程涵  原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 每周实验报告: 反汇编一个简单的C程序 ...

  10. 《Linux内核设计与分析》第四章读书笔记

    <内核设计与实现>第四章读书笔记 第四章:进程调度 进程(操作系统)程序的运行态表现形式. 进程调度程序,它是确保进程能有效工作的一个内核子系统. 调度程序负责决定将哪个进程投入运行,何时 ...