A. Alice and Bob----最大公约数
来源:互联网 发布:网络什么客 编辑:程序博客网 时间:2024/05/22 10:09
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
22 3
Alice
25 3
Alice
35 6 7
Bob
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
题目连接:http://codeforces.com/contest/346/problem/A
题目的意思是说A和B两个人轮流用集合中的两个数做减法取绝对值。减出来的商集合中不能重复,谁不能减了谁就输了。
这个能做出来属于运气,刚拿到这个题一点头绪都没有,想不到和什么知识点有关系,老规矩,先想暴力,我想了bfs暴力,发现虽然只有100个数但是时间复杂度我还是撑不住,然后我试了第三组示例发现1,2,3,4,5,6,7都可以出来,又试了第二组示例,1,2,3,4,5都能出来,又试了2,4,8,发现只能出来2,4,6,8。就猜测与最大公约数有关,试了第一发,用每个数除以最大公约数然后减去已知的数,光荣的wa了,细想之后发现每个数除以最大公约数的话就会有重复,本想hash去重,然后发现直接用最大的数除最大公约数就可以了,然后减去已知的数的个数,然后就没有然后了,ac。
代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int a[1000];int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); int k=a[0]; for(int i=0;i<n;i++){ k=__gcd(k,a[i]);//求n个数的最大公约数 } int sum=0; /*for(int i=0;i<n;i++){ sum+=(a[i]-k)/k; } sum-=n;*/ sum=a[n-1]/k-n; if(sum%2) printf("Alice\n"); else printf("Bob\n"); return 0;}
- A. Alice and Bob----最大公约数
- A:Alice and Bob
- [博弈论]A Game Between Alice and Bob
- zoj3529 A Game Between Alice and Bob
- CF 346A Alice and Bob
- A Game Between Alice and Bob
- [CodeForces 346A]Alice and Bob[数学]
- codeforces-346A-Alice and Bob【数论】
- CodeForces 346A Alice and Bob
- CodeForces 346A Alice and Bob
- Codeforce 346A. Alice and Bob
- Sicily 1732 Alice and Bob (二进制数的最大公约数)
- Alice and Bob
- 2683. Alice and Bob
- 1798. Alice and Bob
- hdu4268 Alice and Bob
- HDU4268 Alice and Bob
- Alice, Bob and Chocolate
- android之seekBar
- 蛇形填数*
- 数据标准化/归一化normalization
- javaweb项目搭建ehcache缓存系统
- SpringMVC自动注入空指针
- A. Alice and Bob----最大公约数
- win10部分软件窗口显示不完整解决办法
- h5自定义滚动条的样式range
- 前端解决跨域问题的八种方案
- 05-非关系型数据库产品介绍
- photoswipe之图片浏览应用
- 重载
- fragment中显示toast
- COURSES