Success Rate

题目链接

题意

给你两个分数形式的数,然后有两种变化方式

  1. 上下都+1
  2. 仅下面部分+1

    让你求第一个分数变化到第二个分数的最小步数。

思路

有几种特殊情况分类讨论一下。

首先我们先把右边化为最简比。

我们可以得到方程$$\frac{(x+a)}{(y+b)} = \frac{kp}{kq}$$

\[a = k*p - x\\b = k*q - y
\]

根据\(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的更多相关文章

  1. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  2. Codeforces Round #412 C. Success Rate

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

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

  4. Codeforces 807 C. Success Rate

    http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...

  5. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  6. CodeForce-807C Success Rate(二分数学)

    Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a< ...

  7. The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  8. Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  9. Codeforces 807C - Success Rate(二分枚举)

    题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...

随机推荐

  1. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  2. 17.Power of Four-Leetcode

    #define IMIN numeric_limits<int>::min() #define IMAX numeric_limits<int>::max() class So ...

  3. 『学了就忘』Linux文件系统管理 — 62、手动分配swap分区

    目录 1.查看swap分区情况 2.手动修改swap分区 3.格式化swap分区 4.使用swap分区 5.配置swap分区开机之后自动挂载 1.查看swap分区情况 swap分区就相当于是内存的一个 ...

  4. IPFS是什么?IPFS原理、IPFS存储

    以下内容调研截止到2021/11/5日 IPFS简介 IPFS是一种内容可寻址.点对点.分布式文件系统.IPFS采用内容-地址寻址技术,即通过文件内容进行检索而不是通过文件的网络地址.简单来说,就是对 ...

  5. 巩固javaweb第十七天

    巩固内容: 文本域 文本域主要用于输入多行文字,如果输入的文字比较多,则可以采用文本域. 文本域的基本格式如下: <textarea rows="行数" name=" ...

  6. KMP算法思路

    题目 给定一个字符串\(S\),求\(M\)字符串是否是\(S\)字符串中的子串.如果是,返回\(M\)对应\(S\)的第一个下标,否则返回-1. 例如:S串为a b c d a b c d a b ...

  7. Angular 组件通信的三种方式

    我们可以通过以下三种方式来实现: 传递一个组件的引用给另一个组件 通过子组件发送EventEmitter和父组件通信 通过serive通信 1. 传递一个组件的引用给另一个组件 Demo1 模板引用变 ...

  8. fastjson转换数字时,格式化小数点

    使用fastjson类库转换java对象时,对于BigDecimal类型,有时需要特殊格式,比如: 1.0,转为json时候,要求显式为1,因此需要在转换时做处理.步骤如下: 1.新建类,实现Valu ...

  9. c学习 - 算法

    简介: 一个程序包括两方面内容:数据结构.算法 数据结构:对数据的描述,包括数据的类型和数据的组织形式 算法:对操作的描述,即操作步骤 (程序=算法+数据结构) 算法是灵魂,数据结构是加工对象,语言是 ...

  10. Spring.DM版HelloWorld

    本文主要描述使用Spring.DM2.0,创建OSGi的HelloWorld演示程序,理解Spring.DM的OSGi框架实现机制.   环境描述: 项目 版本 Eclipse 3.7.x JDK 1 ...