Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?
地址:http://codeforces.com/contest/807/problem/C
题目:
2 seconds
256 megabytes
standard input
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?
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.
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.
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
4
10
0
-1
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.
思路:略
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int p,x,y; int main(void)
{
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
int n;
LL x,y,p,q,x1,x2,ans;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%I64d%I64d%I64d%I64d",&x,&y,&p,&q);
if(p==)
{
if(x!=)
printf("-1\n");
else
printf("0\n");
}
else if(p==q)
{
if(x!=y)
printf("-1\n");
else
printf("0\n");
}
else
{
x1=(q*x-y*p)/p;
if(x1*p<(q*x-y*p))
x1++;
x2=(y*p-x*q)/(q-p);
if(x2*(q-p)<(y*p-x*q))
x2++;
ans=max(x1,x2)+y;
if(ans%q)
ans=((ans/q)+)*q-y;
else
ans=ans-y;
printf("%I64d\n",ans);
} }
return ;
}
Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心
E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)
A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D. Volatile Kite
地址:http://codeforces.com/contest/801/problem/D 题目: D. Volatile Kite time limit per test 2 seconds me ...
随机推荐
- Eclipse & Visual Studio
VS中的解决方案 vs Eclipse中的workspace Maven包管理 vs Nuget类库管理 build path vs
- swift - storyboard(故事版)的使用
iOS开发中,苹果公司提供了一种可视化的编程方式:即xib和storyboard,xib相对来说比较灵活,可以在纯代码的项目中使用, 也可以和storyboard配合使用,用法都差不多,下面来总结一下 ...
- onSaveInstanceState
我们已经分析过Activity的启动流程,从中也分析了Activity的生命周期.而其中有一个生命周期方法:onSaveInstanceState方法,今天我们主要讲解一下onSaveInstance ...
- 动态规划——最长公共上升子序列LCIS
问题 给定两个序列A和B,序列的子序列是指按照索引逐渐增加的顺序,从原序列中取出若干个数形成的一个子集,若子序列的数值大小是逐渐递增的则为上升子序列,若A和B取出的两个子序列A1和B1是相同的,则A1 ...
- JZOJ.5305【NOIP2017模拟8.18】C
Description
- angular 4 路由变化的时候实时监测刷新组件
当路由变化的时候刷新组件 比如说要刷新header组件 在header.ts里 import {Router, NavigationEnd} from "@angular/router&qu ...
- Spring的AOP细节理解
什么是AOP?AOP:是面向切面编程,是对面向对象编程(oop)的一种补充,为什么需要AOP?例如在我们做一个计算器,要求我们每次运行对应的功能(也就是进行运算时)都要输出日志,以便于知道程序是怎么运 ...
- hdu4686 简单的矩阵快速幂求前n项和
HDU4686 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意:题目说的很清楚了,英语不好的猜也该猜懂了,就是求一个表达式的前n项和,矩阵 ...
- Android 判断当前thread 是否是UI thread
在Android 中判断当前的Thread是否是UI Thread 的方法: 1. if (Looper.myLooper() == Looper.getMainLooper()) { // Curr ...
- Win10图标显示不正常解决办法
当缓存文件出现问题时,就会引发系统图标显示不正常: 1.由于图标缓存文件是隐藏文件,我们需要在资源管理器中将设置改为“显示所有文件”. 2.同时按下快捷键 Win+R,在打开的运行窗口中输入 %loc ...