AtCoDeerくんと選挙速報 / AtCoDeer and Election Report AtCoder - 2140 (按比例扩大)
Problem Statement
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.
Input
The input is given from Standard Input in the following format:
- N
- T1 A1
- T2 A2
- :
- TN AN
Output
Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.
Sample Input 1
- 3
- 2 3
- 1 1
- 3 2
Sample Output 1
- 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.
Sample Input 2
- 4
- 1 1
- 1 1
- 1 5
- 1 100
Sample Output 2
- 101
It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.
Sample Input 3
- 5
- 3 10
- 48 17
- 31 199
- 231 23
- 3 2
Sample Output 3
- 6930
- 题意:给你N组数据,每一组数组代表第i秒时两个人的得分比例(最简比例,即分子分母互质),
问你总共最小需要多少个人投票可以满足这N组情况。- 思路:例如当前比例分数是1:1,要比例变成3:2,我们最小的总分只需要5个总数,即投票个数就是(不是比例)3:2.
而如果下一个情况又是1:1,我们想最小总数,只不要再多一个人投第二个人的票,比例就是1:1,而个数是3:3.
我们可以发现要满足某一个情况的比例,我们最小的总人数是上一个比例对应的a和b对当前比例的a和b的向上取整的值再乘以当前比例。
多写几个数据就可以知道这层关系。
还有一个要注意的点是,我们除法向上取整虽然有系统函数ceil,但是容易精度丢失导致答案错误,所以我们自己手写一个ceil函数。
具体的细节看代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <map>
- #include <set>
- #include <vector>
- #include <iomanip>
- #define ALL(x) (x).begin(), (x).end()
- #define rt return
- #define dll(x) scanf("%I64d",&x)
- #define xll(x) printf("%I64d\n",x)
- #define sz(a) int(a.size())
- #define all(a) a.begin(), a.end()
- #define rep(i,x,n) for(int i=x;i<n;i++)
- #define repd(i,x,n) for(int i=x;i<=n;i++)
- #define pii pair<int,int>
- #define pll pair<long long ,long long>
- #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
- #define MS0(X) memset((X), 0, sizeof((X)))
- #define MSC0(X) memset((X), '\0', sizeof((X)))
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define eps 1e-6
- #define gg(x) getInt(&x)
- #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
- using namespace std;
- typedef long long ll;
- ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
- ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
- ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
- inline void getInt(int* p);
- const int maxn=;
- const int inf=0x3f3f3f3f;
- /*** TEMPLATE CODE * * STARTS HERE ***/
- // HFUU-QerM
- // 00:09:24
- ll CEIL(ll a,ll b)
- {
- // 返回a/b并向上取整。
- ll res=a/b;
- if(a%b)
- {
- res++;
- }
- return res;
- }
- int main()
- {
- //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
- //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
- gbtb;
- ll a,b;
- ll n;
- cin>>n;
- cin>>a>>b;
- n--;
- ll x,y;
- repd(i,,n)
- {
- cin>>x>>y;
- ll num=max(CEIL(a,x),CEIL(b,y));
- a=num*x;
- b=num*y;
- }
- cout<<a+b<<endl;
- return ;
- }
- inline void getInt(int* p) {
- char ch;
- do {
- ch = getchar();
- } while (ch == ' ' || ch == '\n');
- if (ch == '-') {
- *p = -(getchar() - '');
- while ((ch = getchar()) >= '' && ch <= '') {
- *p = *p * - ch + '';
- }
- }
- else {
- *p = ch - '';
- while ((ch = getchar()) >= '' && ch <= '') {
- *p = *p * + ch - '';
- }
- }
- }
AtCoDeerくんと選挙速報 / AtCoDeer and Election Report AtCoder - 2140 (按比例扩大)的更多相关文章
- C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report
ceil有毒啊..用ceil一直错. 思路就是模拟吧,设当前的答案是ansx和ansy. 如果比例是小于ansx的,那么就要乘以一个倍数k1,使得a * k1 >= ansx的. 所以就用cei ...
- AtCoDeer and Election Report
问题 G: AtCoDeer and Election Report 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 AtCoDeer the deer is seei ...
- 2018.09.19 atcoder AtCoDeer and Election Report(贪心)
传送门 很有意思的一道贪心. 就是每次翻最小的倍数来满足条件. 代码: #include<bits/stdc++.h> #define ll long long using namespa ...
- AtCoder Regular Contest 062 E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer
题目传送门:https://arc062.contest.atcoder.jp/tasks/arc062_c 题目大意: 给你\(N\)块正方形木板,每块木板四角有四种颜色(可以相同),木板中央有编号 ...
- 【AtCoder】ARC062
ARC062 C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report 每次看看比率至少变成多少倍能大于当前的数 然后就把两个人的票都改成那个数 #incl ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
- FC红白机游戏列表(维基百科)
1055个fc游戏列表 日文名 中文译名 英文版名 发行日期 发行商 ドンキーコング 大金刚 Donkey Kong 1983年7月15日 任天堂 ドンキーコングJR. 大金刚Jr. Donkey K ...
- Python学习 —— 爬虫入门 - 爬取Pixiv每日排行中的图片
更新于 2019-01-30 16:30:55 我另外写了一个面向 pixiv 的库:pixiver 支持通过作品 ID 获取相关信息.下载等,支持通过日期浏览各种排行榜(包括R-18),支持通过 p ...
- [Arc062] Painting Graphs with AtCoDeer
[Arc062] Painting Graphs with AtCoDeer Description 给定一张N点M边的无向图,每条边要染一个编号在1到K的颜色.你可以对一张染色了的图进行若干次操作, ...
随机推荐
- 【响应式编程的思维艺术】 (1)Rxjs专题学习计划
目录 一. 响应式编程 二. 学习路径规划 一. 响应式编程 响应式编程,也称为流式编程,对于非前端工程师来说,可能并不是一个陌生的名词,它是函数式编程在软件开发中应用的延伸,如果你对函数式编程还没有 ...
- 第4章 打包和构建 - Identity Server 4 中文文档(v1.0.0)
IdentityServer由许多nuget包组成. 4.1 IdentityServer4 nuget | github上 包含核心IdentityServer对象模型,服务和中间件.仅包含对内存配 ...
- .NET LINQ 实现跨数据库数据的整合
如果要在不同的数据库之间,要把数据整合到一起,或者对数据进行统计分析的话,实现起来比较麻烦. 一般情况下我们第一时间想到的方法是通过前置机实现,在前置机上安装一个数据库和同步数据程序,定时的把数据同步 ...
- Spring笔记02_注解_IOC
目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...
- Cookie的一点认识!
使用cookie的时间不短了,但是每次都是用一点查一点,也没有总结过.趁着项目不忙的间隙,总结一下.首先说为什么要使用cookie,什么是cookie cookie是用户访问一些网站后,浏览器生成的给 ...
- select2 下拉无法显示
.select2-container--open{ z-index: 99999;}
- 网络最大流算法—EK算法
前言 EK算法是求网络最大流的最基础的算法,也是比较好理解的一种算法,利用它可以解决绝大多数最大流问题. 但是受到时间复杂度的限制,这种算法常常有TLE的风险 思想 还记得我们在介绍最大流的时候提到的 ...
- max-width和width的区别
width为宽度 max-width为最大宽度 如果设置了width,那宽度就定死了,不能动态的改变,显得僵硬 而设置了max-width,实际宽度可以在0~max-width之间,当元素内部宽度不足 ...
- as无法关联git
转载请标明出处:https://www.cnblogs.com/tangZH/p/10060573.html 从gitlab上面把项目拉下来之后,用as打开,发现as无法关联git,没有git相关的菜 ...
- 比较器 comparable与comparator用法
comparable 接口 Comparable<T> 类型参数:T - 可以与此对象进行比较的那些对象的类型 public interface Comparable<T> 此 ...