Nim game n heap matches, each pile has a number of matches, two people take turns, each can choose a pile at least one, can also take the whole heap away, unable to take the person lose.
The number of matches per pile is different or ==0, the initiator loses, otherwise the initiator wins.
http://acm.hust.edu.cn/vjudge/problem/32746 UVA11859
Test instructions: To a 2-D matrix, each time you can select 1 or more integers greater than 1 in a row of a matrix, each of them becomes a true factor. 12 can be changed to 1 2 3 4 6.
Solution: Equivalent to take off one or more elements, a row corresponding to a bunch of matches, each element of each factor as a match, that is, Nim game.
1#include <bits/stdc++.h>2 #defineMT (A, b) memset (A,b,sizeof (a))3 using namespacestd;4 Const intm=1e4+Ten;5 inta[ -][ -];6 Charanswer[2][8]={"NO","YES"};7vector<int>Prime;8 BOOL is[M];9 intn,m;Ten voidinit () { OneMt is,0); A for(intI=2; i*i<m;i++){ - if( is[i])Continue; - for(intj=i*i;j<m;j+=i) { the is[j]=true; - } - } - prime.clear (); + for(intI=2; i<m;i++){ - if( is[i])Continue; + Prime.push_back (i); A } at //printf ("%d", Prime[prime.size ()-1]); - } - intsolve () { - intnim=0; - for(intI=0; i<n;i++){ - intsum=0; in for(intj=0; j<m;j++){ - for(intk=0; K<prime.size (); k++){ to if(Prime[k]>a[i][j])Continue; + while(a[i][j]%prime[k]==0){ -sum++; theA[i][j]/=Prime[k]; * } $ }Panax Notoginseng } -nim^=sum; the } + returnnim!=0; A } the intMain () { + init (); - intT; $ while(~SCANF ("%d",&t)) { $ intcas=1; - while(t--){ -scanf"%d%d",&n,&m); the for(intI=0; i<n;i++){ - for(intj=0; j<m;j++){Wuyiscanf"%d",&a[i][j]); the } - } Wuprintf"Case #%d:%s\n", cas++, Answer[solve ()]); - } About } $ return 0; -}View Code
End
Petition Game Learning