[CQOI2013] New Nim game title description
The traditional NIM game is this: there are some match stacks, each with several matches (the number of matches in different heaps can be different). Two players take turns, each time you can choose a match heap to remove several matches. You can take one or the whole bunch of matches, but not from more than a dozen matches at the same time. The player who took the last match won.
The game is slightly different: in the first round, the first player can take a number of matches directly from the whole pile. Can not take a bunch of, but can not take all. The second round is also the same, the second player also has such an opportunity. Starting with the third turn (and the first player), the rules are the same as the Nim game.
If you take it first, how can you guarantee to win? If you can win, let the first round match the total number of matches as small as possible.
Input/output format
Input format:
The first behavior of the integer k. That is, the number of match stacks.
The second line contains a positive integer of K not exceeding 10^9, that is, the number of matches in each heap.
Output format:
Outputs the minimum number of matches taken in the first round. If there is no guarantee to win, output-1.
Input/Output sample
Input Sample # #:
6
5 5 6 6 5 5
Sample # # of output:
21st
Description
k<=100
Nim Game of the initiator win: a[1] ^ a[2] ^ ... ^ a[n]!=0
So we now need to take a number of stones, so that the opponent regardless of how to take the stone or and not 0.
XOR + is not 0???
Linear Base!!!
The very graceful nature of a linear base: the non-existent \ (0\)in the XOR set of a linear base.
More linear base knowledge, Poke here
So we have a bunch of pebbles, and if the linear base set can make this pile different or out, take the stone.
In order to meet the stone taken away as small as possible, the order is ready.
#include<bits/stdc++.h>#define lll long longusing namespace std;lll read(){ lll x=0,w=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return x*w;}const lll N=110;lll n,ans,a[N],c[N];bool cmp(lll p,lll q){return p>q;}void insert(lll v){ for(lll i=32;i>=0;i--){ if(!(v>>i))continue; if(!c[i]){c[i]=v;break;} v^=c[i];if(!v)break; }}lll find(lll v){ for(lll i=32;i>=0;i--){ if(v>>i)v^=c[i];if(!v)break; }return v;}int main(){ n=read(); for(lll i=1;i<=n;i++)a[i]=read(); sort(a+1,a+1+n,cmp); for(lll i=1;i<=n;i++){ if(find(a[i]))insert(a[i]); else ans+=a[i]; } if(!ans)printf("-1\n"); else printf("%lld\n",ans);}
[CQOI2013] New Nim game (game theory, linear basis)