AtCoDeer and Election Report 二分

6473: AtCoDeer and Election Report

时间限制: 1 Sec  内存限制: 128 MB
提交: 316  解决: 82
[提交][状态][讨论版][命题人:admin]

题目描述

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.

样例输入

3
2 3
1 1
3 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 <bits/stdc++.h>
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<n; i++){
        cin>>a>>b;
        mid=max(x/a,y/b);
        while(mid*a<x || mid*b<y) mid++;
        x=mid*a;
        y=mid*b;
    }
    cout<<x+y<<endl;
    return 0;
}

方法二是用二分的思想

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

代码:

#include <bits/stdc++.h>
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<n; i++){
        cin>>a>>b;
        l=1,r=INF;
        while(l<r){
            ll mid=(l+r)/2;
            if(mid*a>=x && mid*b>=y)  r=mid;
            else  l=mid+1;
        }
        if(l*a<x || l*b<y){
            l=l+max((x-l*a)/a,(y-b*l)/b);
        }
        while(l*a<x || l*b<y)  l++;
        x=l*a;
        y=l*b;
    }
    cout<<x+y<<endl;
    return 0;
}

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值