您的位置:首页 > 理论基础

AtCoder Grand Contest 020 B - Ice Rink Game(贪心+计算机乘法)

2018-01-18 12:35 716 查看
Time limit : 2sec / Memory limit : 512MB

Score : 500 points


Problem Statement

An adult game master and N children
are playing a game on an ice rink. The game consists of K rounds. In the i-th
round, the game master announces:
Form groups consisting of Ai children
each!
Then the children who are still in the game form as many groups of Ai children
as possible. One child may belong to at most one group. Those who are left without a group leave the game. The others proceed to the next round. Note that it's possible that nobody leaves the game in some round.
In the end, after the K-th
round, there are exactly two children left, and they are declared the winners.
You have heard the values of A1, A2,
..., AK.
You don't know N, but you want to estimate it.
Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.


Constraints

1≤K≤105
2≤Ai≤109
All input values are integers.


Input

Input is given from Standard Input in the following format:
K
A1 A2 … AK



Output

Print two integers representing the smallest and the largest possible value of N,
respectively, or a single integer −1 if the described situation is impossible.


Sample Input 1

Copy
4
3 4 3 2



Sample Output 1

Copy
6 8

For example, if the game starts with 6 children,
then it proceeds as follows:
In the first round, 6 children form 2 groups
of 3 children, and nobody leaves the game.
In the second round, 6 children form 1 group
of 4 children, and 2 children
leave the game.
In the third round, 4 children form 1 group
of 3 children, and 1 child
leaves the game.
In the fourth round, 3 children form 1 group
of 2 children, and 1 child
leaves the game.
The last 2 children are declared
the winners.


Sample Input 2

Copy
5
3 4 100 3 2



Sample Output 2

Copy
-1

This situation is impossible. In particular, if the game starts with less than 100 children,
everyone leaves after the third round.


Sample Input 3

Copy
10
2 2 2 2 2 2 2 2 2 2



Sample Output 3

Copy
2 3


题意:有k个游戏,每个游戏每组人数为Ai个,多余的人离开游戏,算出最少和最多在游戏一开始就参加的人数

思路:贪心:

最少:如果是它的倍数则不用更新,如果不是的话,则求出符合条件的最小倍数(和计算机乘除法有关)

最多:比它的整数倍+1组数再减去一个

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
const int N = 100005;
using namespace std;
typedef long long ll;
int a
;
int main()
{

int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
ll imax=2,imin=2;

for(int i=n;i>=1&&imax>=imin;i--){
if(imin%a[i]!=0)
imin=imin/a[i]*a[i]+a[i]; //最小 计算机乘除法处理求出a[i]的整数倍
imax=(imax/a[i]+1)*a[i]-1;    //最大
}
if(imax>imin)
printf("%lld %lld\n",imin,imax);
else
printf("-1\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: