C. Success Rate
Success Rate
题意
给你两个分数形式的数,然后有两种变化方式
- 上下都+1
- 仅下面部分+1
让你求第一个分数变化到第二个分数的最小步数。
思路
有几种特殊情况分类讨论一下。
首先我们先把右边化为最简比。
我们可以得到方程$$\frac{(x+a)}{(y+b)} = \frac{kp}{kq}$$
\]
根据\(b >= a\)可求得\(k = \frac{y-x}{q-p}\)向上取整,并且要求a>=0;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL n,LL m);
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
LL x,y,p,q;
scanf("%lld %lld %lld %lld",&x,&y,&p,&q);
LL ask = 0;
if(x == 0&& p == 0)
ask = 0;
else if(x != 0&&p == 0)
ask = -1;
else
{
LL k;
LL gc = gcd(p,q);
p/=gc,q/=gc;
gc = gcd(x,y);
if(x / gc == p&&y / gc == q)
ask = 0;
else if(p == q&&x!=y)
ask = -1;
else if( p == q&&x == y)ask = 0;
else
{
if((y-x)%(q-p) == 0)
k = (y-x)/(q-p);
else k = (y-x)/(q-p) + 1;
LL a = k*p - x;
LL b = k*q - y;
if(x%p)k = max(x/p+1,k);
else k = max(x/p,k);
if(y%q) k = max(y/q+1,k);
else k = max(y/q,k);
ask = k*q-y;
}
}
printf("%lld\n",ask);
}
return 0;
}
LL gcd(LL n,LL m)
{
if(m == 0)
return n;
else return gcd(m,n%m);
}
C. Success Rate的更多相关文章
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- Codeforces Round #412 C. Success Rate
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces807 C. Success Rate 2017-05-08 23:27 91人阅读 评论(0) 收藏
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 807 C. Success Rate
http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- CodeForce-807C Success Rate(二分数学)
Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a< ...
- The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- Codeforces 807C - Success Rate(二分枚举)
题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...
随机推荐
- Python压缩&解压缩
Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...
- Go语言缺陷
我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...
- do{...}while(0)的用法
零.导引第一次见到 do{...}while(0)是在学习libevent的时候,看到里面有很多类似#define TT_URI(want) do { \ char *ret = evhttp_uri ...
- 日常Java 2021/11/18
用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...
- 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)
1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...
- Ecshop 安装
参考 http://www.68ecshop.com/article-617.html ecshop的安装第一步:下载ecshop网店系统正式版安装包 我们可以来ecshop开发中心的官网(www.6 ...
- pop回指定控制器
//OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...
- [学习总结]8、android 自定义控件 使用declare-styleable进行配置属性(源码角度)
declare-styleable:declare-styleable是给自定义控件添加自定义属性用的. 官方的相关内部控件的配置属性文档:http://developer.android.com/r ...
- 解决Spring MVC @ResponseBody出现问号乱码问题
原因是SpringMVC的@ResponseBody使用的默认处理字符串编码为ISO-8859-1,而我们前台或者客户端的编码一般是UTF-8或者GBK.现将解决方法分享如下! 第一种方法: 对于需要 ...
- 拷贝txt文本中的某行的数据到excel中
package com.hope.day01;import java.io.*;import java.util.ArrayList;public class HelloWorld { publ ...