G58 尼姆(Nim)游戏

视频链接:https://www.bilibili.com/video/BV1ns4y1D7dg/

Luogu P2197 【模板】nim 游戏

 

 

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
  int t,n,x;
  scanf("%d",&t);
  while(t--){
    int s=0; 
    scanf("%d", &n);
    for(int i=0; i<n; i++)
      scanf("%d", &x), s^=x;

    if(s) puts("Yes");
    else puts("No");    
  }
  return 0;
}

Luogu P1247 取火柴游戏

#include<cstdio>
using namespace std;

int k,a[500002];

int main(){
  scanf("%d",&k); 
  int s=0;
  for(int i=1;i<=k;i++)
    scanf("%d",&a[i]), s^=a[i];
    
  if(!s) return puts("lose"),0;
  for(int i=1;i<=k;i++){
    if((a[i]^s)>=a[i]) continue;
    printf("%d %d\n",(a[i]-(a[i]^s)),i);
    a[i]=a[i]^s; break;
  }
  for(int i=1;i<=k;i++) printf("%d ",a[i]);
  return 0;
}

 

Luogu P1288 取数游戏 II

#include<cstdio>
#define check(v) (v&1)
using namespace std;

int n,s[1000];

int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++) scanf("%d",&s[i]);
  int a=0; while(s[++a]);
  int b=0; while(s[n+1-(++b)]);
  if(check(--a)||check(--b)) printf("YES");  
  else printf("NO");
  return 0;
}

Luogu P4279 [SHOI2008]小约翰的游戏

#include<cstdio>
using namespace std;

int main(){
int T,n,x; scanf("%d", &T);
while(T--){
  scanf("%d", &n); int ans=0, sum=0;
  for(int i=1; i<=n; ++i)scanf("%d",&x), ans^=x, sum+=x;
  if(sum==n) puts(n&1 ? "Brother" : "John");
  else puts(ans ? "John" : "Brother");
}
return 0;
}

Luogu P1199 [NOIP2010 普及组] 三国游戏

#include<cstdio>
#include<algorithm>
using std::sort;
int a[510][510];
int main(){
  int n; scanf("%d",&n);
  for(int i=1;i<n;i++)
    for(int j=i+1;j<=n;j++)
      scanf("%d",&a[i][j]), a[j][i]=a[i][j];

  int ans=0;
  for(int i=1;i<=n;i++){
    sort(a[i]+1,a[i]+1+n);
    ans = ans>a[i][n-1]?ans:a[i][n-1];
  }
  printf("1\n%d\n",ans);
  return 0;
}

Luogu P1290 欧几里德的游戏

Luogu P2252 [SHOI2002]取石子游戏|【模板】威佐夫博弈

 

posted @ 2023-03-27 21:33  董晓  阅读(342)  评论(0编辑  收藏  举报