博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AtCoDeer and Election Report 二分
阅读量:5323 次
发布时间:2019-06-14

本文共 2120 字,大约阅读时间需要 7 分钟。

6473: AtCoDeer and Election Report

时间限制: 1 Sec  内存限制: 128 MB

提交: 316  解决: 82
[][][][命题人:]

题目描述

AtCoDeer the deer is seeing a quick report of election results on TV. Two candidates are standing for the election: Takahashi and Aoki. The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes. AtCoDeer has checked the report N times, and when he checked it for the i-th (1≤i≤N) time, the ratio was Ti:Ai. It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time. It can be assumed that the number of votes obtained by each candidate never decreases.
Constraints
1≤N≤1000
1≤Ti,Ai≤1000(1≤i≤N)
Ti and Ai (1≤i≤N) are coprime.
It is guaranteed that the correct answer is at most 1018.

输入

The input is given from Standard Input in the following format:

N
T1 A1
T2 A2
:
TN AN

输出

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

样例输入

32 31 13 2

样例输出

10

提示

When the numbers of votes obtained by the two candidates change as 2,3→3,3→6,4, the total number of votes at the end is 10, which is the minimum possible number.

寻找最小的满足比例的数x与y

因为是比例问题,所以计算每一份的数量比较方便

方法一是类似直接枚举,先通过之前确定的x与y分别除a,b(即文章里的t ,a),取较大值,然后判断是否满足比例

代码:

#include 
using namespace std;typedef long long ll;const ll MAX=1e16;const int INF=1e6+100;int main(){ int n,a,b; cin>>n; ll x=1,y=1; ll r,l,mid; for(int i=0; i
>a>>b; mid=max(x/a,y/b); while(mid*a

方法二是用二分的思想

每次二分计算符合比例的最小值,并且二分后要加判断条件确定是否满足比例

代码:

#include 
using namespace std;typedef long long ll;const ll MOD=1e9+7;const ll INF=1e16;int main(){ int n; cin>>n; ll x=1,y=1; ll a,b; ll r,l; for(int i=0; i
>a>>b; l=1,r=INF; while(l
=x && mid*b>=y) r=mid; else l=mid+1; } if(l*a

 

转载于:https://www.cnblogs.com/renxiaomiao/p/9642703.html

你可能感兴趣的文章
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
Leetcode 589. N-ary Tree Preorder Traversal
查看>>
正则表达式
查看>>
arcgis api 4.x for js 结合 Echarts4 实现散点图效果(附源码下载)
查看>>
YTU 2625: B 构造函数和析构函数
查看>>
apache自带压力测试工具ab的使用及解析
查看>>
加固linux
查看>>
10.17动手动脑
查看>>
WPF中Image显示本地图片
查看>>
Java多线程基础(一)
查看>>
SQL Server中利用正则表达式替换字符串
查看>>
[poj1006]Biorhythms
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
Hover功能
查看>>
js千分位处理
查看>>
Mac---------三指拖移
查看>>
字符串类型的相互转换
查看>>
HTTP状态码
查看>>
iOS如何过滤掉文本中特殊字符
查看>>
基础学习:C#中float的取值范围和精度
查看>>