• Nim or not Nim? HDU


    题目链接:https://vjudge.net/problem/HDU-3032

    题意:有n堆物品,你可以有两种操作1.每次拿一堆中任意数量(大于1)的物品;2.选一堆进行拆分(每一堆大于0)。拿到最后一个物品的人获胜。

    思路:这题其实是尼姆游戏的变形,我们可以打表sg函数找规律发现,当x%4==0 sg[x]=x-1,当x%4==3 sg[x]=x+1,其他情况sg[x]=x;

      1 //#include<bits/stdc++.h>
      2 #include<time.h>
      3 #include <set>
      4 #include <map>
      5 #include <stack>
      6 #include <cmath>
      7 #include <queue>
      8 #include <cstdio>
      9 #include <string>
     10 #include <vector>
     11 #include <cstring>
     12 #include <utility>
     13 #include <cstring>
     14 #include <iostream>
     15 #include <algorithm>
     16 #include <list>
     17 using namespace std;
     18 #define eps 1e-10
     19 #define PI acos(-1.0)
     20 #define lowbit(x) ((x)&(-x))
     21 #define zero(x) (((x)>0?(x):-(x))<eps)
     22 #define mem(s,n) memset(s,n,sizeof s);
     23 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
     24 typedef long long ll;
     25 typedef unsigned long long ull;
     26 const int maxn=5e6+5;
     27 const int Inf=0x7f7f7f7f;
     28 const ll Mod=999911659;
     29 const int N=3e3+5;
     30 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
     31 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
     32 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
     33 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
     34 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
     35 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
     36 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
     37 int Abs(int n) {
     38   return (n ^ (n >> 31)) - (n >> 31);
     39   /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
     40      若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
     41      需要计算 n 和 -1 的补码,然后进行异或运算,
     42      结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
     43 }
     44 ll binpow(ll a, ll b,ll c) {
     45   ll res = 1;
     46   while (b > 0) {
     47     if (b & 1) res = res * a%c;
     48     a = a * a%c;
     49     b >>= 1;
     50   }
     51   return res%c;
     52 }
     53 void extend_gcd(ll a,ll b,ll &x,ll &y)
     54 {
     55     if(b==0) {
     56         x=1,y=0;
     57         return;
     58     }
     59     extend_gcd(b,a%b,x,y);
     60     ll tmp=x;
     61     x=y;
     62     y=tmp-(a/b)*y;
     63 }
     64 ll mod_inverse(ll a,ll m)
     65 {
     66     ll x,y;
     67     extend_gcd(a,m,x,y);
     68     return (m+x%m)%m;
     69 }
     70 ll eulor(ll x)
     71 {
     72    ll cnt=x;
     73    ll ma=sqrt(x);
     74    for(int i=2;i<=ma;i++)
     75    {
     76     if(x%i==0) cnt=cnt/i*(i-1);
     77     while(x%i==0) x/=i;
     78    }
     79    if(x>1) cnt=cnt/x*(x-1);
     80    return cnt;
     81 }
     82 int a[maxn];
     83 int sg[maxn];
     84 int vis[maxn];
     85 /*
     86 打表求SG的函数: 
     87 void cal(){
     88     sg[0]=0;
     89     sg[1]=1;
     90     int j;
     91     for(int i=2; i<=1000; i++){
     92          mem(vis,0);
     93         for( j=1;j<i;j++) vis[sg[j]^sg[i-j]]=1;
     94         for( j=0;j<i;j++) vis[sg[j]]=1;
     95         for( j=0;;j++)
     96         if(!vis[j]) break;
     97         sg[i]=j;
     98     }
     99     for(int i=1;i<=20;i++)
    100         cout<<sg[i]<<endl;
    101 }
    102 */
    103 int main()
    104 {   
    105     //cal();
    106     int t;
    107     int n;
    108     ios
    109     cin>>t;
    110     while(t--)
    111     {
    112         cin>>n;
    113         int x;
    114         for(int i=0;i<n;i++)
    115         {
    116             cin>>x;
    117             if(x%4==3) sg[i]=x+1;
    118             else if(x%4==0) sg[i]=x-1;
    119             else sg[i]=x;
    120         }
    121         int tmp=0;
    122         for(int i=0;i<n;i++)
    123         {
    124             tmp^=sg[i];
    125         }
    126         if(tmp) puts("Alice");
    127         else puts("Bob");
    128     }
    129     return 0;
    130 }
    View Code
  • 相关阅读:
    LeetCode——打家劫舍问题
    LeetCode——无重叠区间
    LeetCode——岛屿数量
    LeetCode——石子游戏
    LeetCode——最长回文子序列
    LeetCode——高层扔鸡蛋*
    LeetCode——零钱兑换
    LeetCode刷题之BFS和DFS
    没有学历,四步加入谷歌
    坚持编程
  • 原文地址:https://www.cnblogs.com/zpj61/p/13535858.html
Copyright © 2020-2023  润新知