HDU 3404&poj 3533 Nim product (two-dimensional & three-dimensional)

Source: Internet
Author: User

(Nim product related data from the paper Cao Chinxiang "k times Dynamic subtraction game" to explore a class of combinatorial game problems)

Two function flows for the NIM product calculation:

The code is implemented as follows:

intm[2][2]={0,0,0,1};intNim_multi_power (intXinty) {    if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,s=y/m,t=y%m; intd1=Nim_multi_power (p,s); intD2=Nim_multi_power (p,t); return(m* (D1^D2)) ^nim_multi_power (m/2, D1);}intNim_multi (intXinty) {    if(x<y)returnNim_multi (y,x); if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,q=x%m,s=y/m,t=y%m; intc1=Nim_multi (p,s); intC2=nim_multi (p,t) ^Nim_multi (q,s); intc3=Nim_multi (q,t); return(m* (C1^C2)) ^c3^nim_multi_power (m/2, C1);}

Here are two game problems that are implemented with the NIM product:

(1) HDU 3404 Switch lights:http://acm.hdu.edu.cn/showproblem.php?pid=3404

#include <iostream>#include<cstdio>using namespacestd;intm[2][2]={0,0,0,1};intNim_multi_power (intXinty) {    if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,s=y/m,t=y%m; intd1=Nim_multi_power (p,s); intD2=Nim_multi_power (p,t); return(m* (D1^D2)) ^nim_multi_power (m/2, D1);}intNim_multi (intXinty) {    if(x<y)returnNim_multi (y,x); if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,q=x%m,s=y/m,t=y%m; intc1=Nim_multi (p,s); intC2=nim_multi (p,t) ^Nim_multi (q,s); intc3=Nim_multi (q,t); return(m* (C1^C2)) ^c3^nim_multi_power (m/2, C1);}intMain () {intT,n,x,y; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); intres=0;  while(n--) {scanf ("%d%d",&x,&y); Res^=Nim_multi (x, y); }        if(res) printf ("There is a try, lxhgww.\n"); Elseprintf"Don ' t waste your time.\n"); }    return 0;}

(2) POJ 3533 Light switching game:http://poj.org/problem?id=3533

The previous question is a two-dimensional NIM product, this problem is different from the three-dimensional NIM product, but the same nature.

#include <iostream>#include<cstdio>using namespacestd;intm[2][2]={0,0,0,1};intNim_multi_power (intXinty) {    if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,s=y/m,t=y%m; intd1=Nim_multi_power (p,s); intD2=Nim_multi_power (p,t); return(m* (D1^D2)) ^nim_multi_power (m/2, D1);}intNim_multi (intXinty) {    if(x<y)returnNim_multi (y,x); if(x<2)        returnM[x][y]; intA=0;  for(;; a++)        if(X>= (1<< (1<<a)) &&x< (1<< (1<< (A +1))))             Break; intm=1<< (1<<a); intp=x/m,q=x%m,s=y/m,t=y%m; intc1=Nim_multi (p,s); intC2=nim_multi (p,t) ^Nim_multi (q,s); intc3=Nim_multi (q,t); return(m* (C1^C2)) ^c3^nim_multi_power (m/2, C1);}intNim_multi2 (intXintYintz) {    intt=Nim_multi (x, y); returnNim_multi (t,z);}intMain () {intn,x,y,z;  while(~SCANF ("%d",&N)) {intres=0;  while(n--) {scanf ("%d%d%d",&x,&y,&z); Res^=nim_multi2 (x, y, z); }        if(res) printf ("no\n"); Elseprintf"yes\n"); }    return 0;}

HDU 3404&poj 3533 Nim product (two-dimensional & three-dimensional)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.