Monsters Battle Royale
Problem Statement
There are N monsters, numbered 1,2,…,N.
Initially, the health of Monster i is Ai.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
- A random alive monster attacks another random alive monster.
- As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
Find the minimum possible final health of the last monster alive.
Constraints
- All values in input are integers.
- 2≤N≤105
- 1≤Ai≤109
Input
Input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the minimum possible final health of the last monster alive.
Sample Input 1
4
2 10 8 40
Sample Output 1
2
When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.
Sample Input 2
4
5 13 8 1000000000
Sample Output 2
1
Sample Input 3
3
1000000000 1000000000 1000000000
Sample Output 3
1000000000
要求找到最后一个活着的怪物的可能最终最小的生命值。就是不断辗转相减嘛(比如第一个样例:40血的怪兽可以被10血的怪兽经四次消灭掉,同理10血,2血的都会被2血的灭掉,所以最后只剩2血的,其他攻击方法结果也是这样。再举个没有倍数关系的:如2和5,让2血攻击5血的,剩2血的和3血;再次让2攻击3血的,剩1血和2血;最后让1血攻击2血;最后就是俩1血的,让一个攻击另一个,最后只能剩下一个1血的。),就是求所有数的最小公倍数就好了。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;;
int main()
{
int n;
cin >> n;
int a[maxn];
for(int i = 0;i < n;i ++){
cin >> a[i];
}
int ans = a[0];
for(int i = 1;i < n;i ++){
ans = __gcd(ans,a[i]);
}
cout << ans << endl;
return 0;
}