两个AC解 - 作弊中头奖(Hit the Jackpot)

本文探讨了如何通过编程计算老虎机的周期性,即在不出现特定组合之前,机器需要被玩多少次。通过理解每个轮盘上特定符号出现的频率,我们可以计算出机器在连续两次出现特定组合之间的次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题: http://poj.org/problem?id=3002



Description
Bill has found the perfect way to make money playing the slot machines. After months of careful research, he has finally figured out the mechanics behind how the machines operate. Now he is ready to make profit of his findings.

But first an introduction to the game. A slot machine consists of a number of wheels, usually three or four, each with a number of symbols printed on it – cherries, oranges, bells, etc. – and will show one of its symbols at a given time. To play, you insert a coin, push a button and the wheels start spinning. After spinning for a while, each wheel stops – at random it seems – at one of its symbols. If all wheels stop at the same symbol, or some nice combination of symbols, the player wins. One combination that is especially desirable is having the jackpot symbol on all wheels. This combination is simply called ’jackpot’ and will make you rich for life.

What Bill has discovered is that each wheel will stop at the jackpot symbol with a certain periodicity, which differs a lot between wheels. He has also figured out (after some sneeking around at the slot-machine factory) that all newly manufactured slot-machines are delivered showing the jackpot combination, and that they all have a counter at the back, telling how many times the machine has been played. This counter is always set to zero at delivery.

Now, all Bill needs to do is to calculate the number of times a machine has to be played between two occurrences of the jackpot combination. We will call this number the jackpot periodicity. This is of course the same as the number of times the machine has to be played after leaving the factory, before it gives its first jackpot. Thus, with a glance at the counter on the back of a machine, Bill can figure out if it is about to give a jackpot.

As Bill knows that you are a skillful computer programmer, he turns to you with the problem of calculating the jackpot periodicity. For each machine, he will give you the number of wheels, and the periodicity with which the jackpot symbol shows up on each wheel.

Input
One line with the number of machines n ≤ 20. For each machine, one line with the number of wheels w ≤ 5, and one line with w numbers, p1 , ..., pw the periodicity of each wheel pk ≤ 1000.

Output
One line per machine: The jackpot periodicity of the machine, if it is less than or equal to a billion (109 ), otherwise output the text ’More than a billion.’.

Sample Input

1

3

10 6 15



分析:问题实际上就是求n个整数的最小公倍数。解题思路参考笔者另外一篇博文“求k-1个数的最小公倍数”。

*****************
解法1: 质因素分解

 

 

 

 

*****************
解法2: 通过gcd求lcm

 

 

以上两个都是AC解。

 

 

 

类似的一道求LCM题:http://acm.hdu.edu.cn/showproblem.php?pid=1019

 

该题只能用gcd的方法,才能AC(poj的题因为限制w ≤ 5所以质因数分解耗时不会太多):

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值