zoukankan      html  css  js  c++  java
  • luogu P2575 高手过招

    嘟嘟嘟


    这题刚开始不太会,学姐也没什么好方法,题解说的什么阶梯NIM又不懂……
    这时候学姐说,干脆直接记搜吧,反正就(O(2 ^ {20}))


    首先这道题,每一行都是一个独立的游戏,所以整个游戏的sg函数应该是每一行的sg函数的异或和。
    每一行的sg函数就是初始状态对应的sg函数。
    观察到这是一个“无向图游戏”(详见《算法竞赛进阶指南》),所以我们可以暴力的枚举所有状态,那么这个状态的sg函数就是他能到达的状态的sg函数的mex值。


    所以我们(O(2 ^ {20}))预处理所有状态,然后就可以(O(1))回答询问了。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<assert.h>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    //const int maxn = ;
    In ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    In void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
      freopen(".in", "r", stdin);
      freopen(".out", "w", stdout);
    #endif
    }
    
    int n;
    
    int dp[(1 << 20) + 5];
    int Max = 0;
    In void dfs(int S)
    {
      if(~dp[S]) return;
      bool flg = 0;
      for(int i = 1; i < 20; ++i)
        if((S >> (i - 1)) & 1)
          {
    	if(!((S >> i) & 1))
    	  dfs((S ^ (1 << i)) ^ (1 << (i - 1))), flg = 1;
    	else
    	  {
    	    for(int j = i + 2; j <= 20; ++j)
    	      if(!((S >> (j - 1)) & 1))
    		{
    		  dfs((S ^ (1 << (i - 1))) ^ (1 << (j - 1)));
    		  flg = 1; break;
    		}
    	  }
          }
      if(!flg) {dp[S] = 0; return;}
      static int a[50]; int tot = 0;
      for(int i = 1; i < 20; ++i)
        if((S >> (i - 1)) & 1)
          {
    	if(!((S >> i) & 1))
    	  a[++tot] = dp[(S ^ (1 << i)) ^ (1 << (i - 1))];
    	else
    	  {
    	    for(int j = i + 2; j <= 20; ++j)
    	      if(!((S >> (j - 1)) & 1))
    		{
    		  a[++tot] = dp[(S ^ (1 << (i - 1))) ^ (1 << (j - 1))];
    		  break;
    		}
    	  }
          }
      sort(a + 1, a + tot + 1);
      a[0] = -1, a[tot + 1] = INF;
      for(int i = 0; i <= tot && dp[S] == -1; ++i) if(a[i] + 1 < a[i + 1]) dp[S] = a[i] + 1;
    }
    
    In void init()
    {
      Mem(dp, -1);
      for(int i = 0; i < (1 << 20); ++i) dfs(i);
    }
    
    int main()
    {
      //MYFILE();
      init();
      int T = read();
      while(T--)
        {
          n = read(); int ans = 0;
          for(int i = 1; i <= n; ++i)
    	{
    	  int m = read(), S = 0;
    	  for(int j = 1; j <= m; ++j) S |= (1 << (read() - 1));
    	  ans ^= dp[S];
    	}
          puts(ans ? "YES" : "NO");
        }
      return 0;
    }
    
  • 相关阅读:
    [PKUWC2018][LOJ2537]Minimax(线段树合并)
    [NOI2019][洛谷P5471]弹跳(dijkstra+KD-Tree)
    [BZOJ4770]图样(概率期望、二进制数位dp)
    [SPOJ11482][BZOJ2787]Count on a trie(广义SA+长链剖分+BIT)
    [HEOI/TJOI2016][洛谷P4094]字符串(SA+主席树)
    [BZOJ3270]博物馆(矩阵求逆)
    [NOI2016][洛谷P1117]优秀的拆分(SA)
    [NOI2018][洛谷P4770]你的名字(SAM+SA+主席树)
    设置echarts两个y轴的0点一致
    echarts中饼图或环形图的高亮效果(点击高亮/默认某一条高亮)
  • 原文地址:https://www.cnblogs.com/mrclr/p/10906380.html
Copyright © 2011-2022 走看看