Nim(Nim博弈變形)

來源:互聯網
上載者:User


Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4675


Nim Time Limit: 3 Seconds       Memory Limit: 65536 KB

Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. Here is another version of Nim game. There are N piles of stones on the table. Alice first chooses some CONSECUTIVE piles of stones to play the Nim game with Tom. Also, Alice will make the first move. Alice wants to know how many ways of choosing can make her win the game if both players play optimally.

You are given a sequence a[0],a[1], ... a[N-1] of positive integers to indicate the number of stones in each pile. The sequence a[0]...a[N-1] of length N is generated by the following code:

  
int g = S;  for (int i=0; i<N; i++) {      a[i] = g;     if( a[i] == 0 ) { a[i] = g = W; }     if( g%2 == 0 ) { g = (g/2); }     else           { g = (g/2) ^ W; } }

Input

There are multiple test cases. The first line of input is an integer T(T ≤ 100) indicates the number of test cases. Then T test cases follow. Each test case is represented by a line containing 3 integers N, S and W, separated by spaces. (0 < N ≤ 105, 0 < S, W ≤ 109) Output

For each test case, output the number of ways to win the game. Sample Input

23 1 13 2 1
Sample Output
45
Author:  CAO, Peng
Contest:  The 12th Zhejiang University Programming Contest


編程思想:博弈+求逆反面思想


AC code:

#include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>#include<queue>#include<map>#include<set>#define LL long long#define M 1100010using namespace std;int a[100100],b[100100];LL ans,dd;int main(){int N,S,W,T;scanf("%d",&T);while(T--){scanf("%d%d%d",&N,&S,&W);int g = S; for (int i=0; i<N; i++) {     a[i] = g;    if( a[i] == 0 ) { a[i] = g = W; }    if( g%2 == 0 ) { g = (g/2); }    else           { g = (g/2) ^ W; }}ans=(LL)(1+N)*N/2;//必須強加LL轉換為long long,否則 (1+N)*N/2超int型,而N是int,所以會溢出int,導致WA。。。 int tmp=0;for(int i=0;i<N;i++){tmp^=a[i];b[i]=tmp;}sort(b,b+N);int bb=b[0];dd=0;LL cnt=1;for(int i=1;i<N;i++){if(bb==b[i]){cnt++;}else{if(bb==0) dd+=cnt;//為0的話,從第0堆到i堆這一組是必敗局面,要減掉 dd+=(LL)cnt*(cnt-1)/2;cnt=1;bb=b[i];}}dd+=(LL)cnt*(cnt-1)/2;ans-=dd;printf("%lld\n",ans);}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.