HDU-2176 取(m堆)石子游戏

http://acm.hdu.edu.cn/showproblem.php?pid=2176

第三种博弈,但一定要注意优化时间

                    取(m堆)石子游戏

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1273    Accepted Submission(s): 760

Problem Description
m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎样取子.例如5堆 5,7,8,9,10先取者胜,先取者第1次取时可以从有8个的那一堆取走7个剩下1个,也可以从有9个的中那一堆取走9个剩下0个,也可以从有10个的中那一堆取走7个剩下3个.
 
Input
输入有多组.每组第1行是m,m<=200000. 后面m个非零正整数.m=0退出.
 
Output
先取者负输出No.先取者胜输出Yes,然后输出先取者第1次取子的所有方法.如果从有a个石子的堆中取若干个后剩下b个后会胜就输出a b.参看Sample Output.
 
Sample Input
2
45 45
3
3 6 9
5
5 7 8 9 10
0
 
Sample Output
No
Yes
9 5
Yes
8 1
9 0
10 3
 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,i,t,k;
 7     int a[200005];
 8     while(~scanf("%d",&n)&&n!=0)
 9     {
10             t=0;
11         for(i=0;i<n;i++)
12         {
13             scanf("%d",&a[i]);
14             t=t^a[i];
15         }
16         if(t!=0)
17         {
18             printf("Yes\n");
19            for(i=0;i<n;i++)
20            {
21                k=t^a[i];//怎样优化时间
22               if(k<a[i])
23                 printf("%d %d\n",a[i],k);
24          }
25         }
26         else
27             printf("No\n");
28     }
29     return 0;
30 }

 

posted @ 2014-01-10 19:58  疯狂的癫子  阅读(248)  评论(0编辑  收藏  举报