取(m堆)石子游戏
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1158 Accepted Submission(s): 692
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
Author
Zhousc
Source
时间复杂度O(n)......
{
方法很简单,因为我们要求的除了该值,还有其他所有值的异或值
在判断奇异状态时候,已经求了全部的异或值,
比如a,b,c t=a^b^c 那么此时要求取 b^c=t^a;
同理...
}
java版:
package 暑期培训_2013_08_04;
import java.util.Scanner;
public class hdu_2176_取_2堆_石子游戏_博弈_201308231718 {
//Accepted 2176 781MS 5256K 569 B Java 1983210400
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc =new Scanner(System.in);
while(true){
int N =sc.nextInt();
int [] a = new int[N+1];
int [] f = new int[N+1];
if(N==0)break;
int t=0;
for(int i=1; i<=N; i++){
a[i] = sc.nextInt();
t ^= a[i];
}
if(t==0){
System.out.println("No");
continue;
}
System.out.println("Yes");
for(int i=1; i<=N; i++){
f[i]= t^a[i];
if(f[i]<=a[i])
System.out.println(a[i]+" "+f[i]);
}
}
}
}
C语言版本:
#include <stdio.h>
//2013-08-23 17:11:57 Accepted 2176 93MS 980K 335 B G++
int a[200000];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int i;
int sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum^a[i];
}
if(sum==0)
{
printf("No\n");
continue;
}
printf("Yes\n");
for(i=0;i<n;i++)
{
int p=sum^a[i];
if(a[i]>=p)
printf("%d %d\n",a[i],p);
}
}
return 0;
}