AtCoder Grand Contest 020 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组数再减去一个

#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[N];
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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting) 是一道比较有趣的题目。它是一道交互式的排序题目,需要你与一个神秘程序进行交互,以便将一串无序的数字序列排序。 具体来说,这个神秘程序会给你一个长度为 $N$ 的数字序列,然后你需要通过询问它两个数字的大小关系,来逐步确定这个序列的排序顺序。每次询问之后,神秘程序都会告诉你两个数字的大小关系,比如第一个数字比第二个数字小,或者第二个数字比第一个数字小。你需要根据这个信息,来调整这个数字序列的顺序,然后再向神秘程序询问下一对数字的大小关系,以此类推,直到这个数字序列被完全排序为止。 在这个过程中,你需要注意以下几点: 1. 你最多只能向神秘程序询问 $Q$ 次。如果超过了这个次数,那么你的程序会被判定为错误。 2. 在每次询问之后,你需要及时更新数字序列的顺序。具体来说,如果神秘程序告诉你第 $i$ 个数字比第 $j$ 个数字小,那么你需要将这两个数字交换位置,以确保数字序列的顺序是正确的。如果你没有及时更新数字序列的顺序,那么你的程序也会被判定为错误。 3. 在询问的过程中,你需要注意避免重复询问。具体来说,如果你已经询问过第 $i$ 个数字和第 $j$ 个数字的大小关系了,那么你就不需要再次询问第 $j$ 个数字和第 $i$ 个数字的大小关系,因为它们的大小关系已经被确定了。 4. 在排序完成之后,你需要将排序结果按照从小到大的顺序输出。如果你输出的结果不正确,那么你的程序也会被判定为错误。 总的来说,这道题目需要你熟练掌握交互式程序设计的技巧,以及排序算法的实现方法。如果你能够熟练掌握这些技巧,那么就可以顺利地完成这道非传统题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值