AtCoder Grand Contest 020 B - Ice Rink Game

B - Ice Rink Game


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 A1A2, ..., 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

  • 1K105
  • 2Ai109
  • 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.tourist的题真心不错,数论题,我错了好几次,刚开始以为控制最大最少人数的只是因为最后一组数据,后来才知道中间也存在着一些变数,所以后来分开写了,判断有没有可能只要看两个值的大小是否满足即可


#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
int main() {
    int k;
    scanf("%d",&k);
    for (int i=1; i<=k; i++) {
        scanf("%d",&a[i]);
    }
    long long mmax=2,mmin=2;
    for(int i=k;i>=1&&mmax>=mmin;i--)
    {
        if(mmin%a[i]!=0)
            mmin=mmin/a[i]*a[i]+a[i];
        mmax=(mmax/a[i]+1)*a[i]-1;
    }
    if(mmax>=mmin)
        printf("%lld %lld\n",mmin,mmax);
    else
        printf("-1\n");
    // insert code here...
    //std::cout << "Hello, World!\n";
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值