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),取较大值,然后判断是否满足比例
代码:
#includeusing 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
方法二是用二分的思想
每次二分计算符合比例的最小值,并且二分后要加判断条件确定是否满足比例
代码:
#includeusing 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