Ice Rink Game
题目描述
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≤1e5
2≤Ai≤1e9
All input values are integers.
输入
Input is given from Standard Input in the following format:
K
A1 A2 … AK
输出
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.
样例输入
4
3 4 3 2
样例输出
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.
这就是一道模拟题
看到这个问题就很自然而然地想到以前体育课玩的游戏,老师喊一个数,比如说5,那么就有5个同学分为一组,剩下的没有分为一组的同学将会淘汰,和这个题的叙述是一模一样的,这道题给出的数字就相当于老师喊的数字,求开始最多有多少人,最少有多少人。一开始由于最大最小不确定,就都定义为2(最后剩余的人数)。
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int num[maxn];
ll maxx=2,minn=2;
start{
int k=read;
for (int i=1; i<=k; i++)
num[i]=read;
for(int i=k;i>=1&&maxx>=minn;i--)
{
if(minn%num[i]!=0)///不能被整除的情况下才有可能会淘汰
minn=minn/num[i]*num[i]+num[i];
maxx=(maxx/num[i]+1)*num[i]-1;///最大值要上取整最后减一
}
if(maxx>=minn)
printf("%lld %lld
",minn,maxx);
else///若求得的最大值小于最小值,那么来说,这就是不存在的情况
printf("-1
");
end;
}