• [zoj3591]Nim 游戏


    题意:有n堆火柴,选择连续若干堆火柴进行Nim游戏,求让先手胜的选择方案数。

    思路:让先手胜等同于这些数的异或值不同于0,不妨转化为求让先手败的方案数。此时记录一个前缀的异或和val[i],那么答案就是count({i,j})(0<=i<j<n,val[i]=val[j])+count(i)(val[i]=0)。直接map统计可能超时,不妨考虑离线做,把val数组sort一下答案就不难得到了,不要忘记最后用总方案数减一下。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21 
     22 using namespace std;
     23 
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define mem_1(a) memset(a, -1, sizeof(a))
     26 #define lson l, m, rt << 1
     27 #define rson m + 1, r, rt << 1 | 1
     28 #define define_m int m = (l + r) >> 1
     29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     33 #define all(a) (a).begin(), (a).end()
     34 #define lowbit(x) ((x) & (-(x)))
     35 #define constructInt5(name, a, b, c, d, e) name(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0): a(a), b(b), c(c), d(d), e(e) {}
     36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     39 #define pchr(a) putchar(a)
     40 #define pstr(a) printf("%s", a)
     41 #define sstr(a) scanf("%s", a)
     42 #define sint(a) scanf("%d", &a)
     43 #define sint2(a, b) scanf("%d%d", &a, &b)
     44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     45 #define pint(a) printf("%d
    ", a)
     46 #define test_print1(a) cout << "var1 = " << a << endl
     47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     49 #define mp(a, b) make_pair(a, b)
     50 #define pb(a) push_back(a)
     51 
     52 typedef long long LL;
     53 typedef pair<int, int> pii;
     54 typedef vector<int> vi;
     55 
     56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     58 const int maxn = 3e4 + 7;
     59 const int md = 10007;
     60 const int inf = 1e9 + 7;
     61 const LL inf_L = 1e18 + 7;
     62 const double pi = acos(-1.0);
     63 const double eps = 1e-6;
     64 
     65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     68 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     70 int make_id(int x, int y, int n) { return x * n + y; }
     71 
     72 int g, s, n, w, a[100007], nim[100007];
     73 
     74 void init() {
     75     g = s;
     76     rep_up0(i, n) {
     77         a[i] = g;
     78         if (a[i] == 0) {
     79             a[i] = g = w;
     80         }
     81         if (g % 2 == 0) {
     82             g /= 2;
     83         }
     84         else g = (g / 2) ^ w;
     85     }
     86 }
     87 
     88 int main() {
     89     //freopen("in.txt", "r", stdin);
     90     int T;
     91     cin >> T;
     92     while (T --) {
     93         cin >> n >> s >> w;
     94         init();
     95         nim[0] = a[0];
     96         rep_up0(i, n - 1) nim[i + 1] = nim[i] ^ a[i + 1];
     97         sort(nim, nim + n);
     98         LL ans = 0;
     99         rep_up0(i, n) {
    100             if (nim[i] != 0) break;
    101             ans ++;
    102         }
    103         LL c = 1;
    104         nim[n] = -1;
    105         rep_up0(i, n) {
    106             if (nim[i] != nim[i + 1]) {
    107                 ans += c * (c - 1) / 2;
    108                 c = 1;
    109             }
    110             else c ++;
    111         }
    112         cout << (LL)n * (n + 1) / 2 - ans << endl;
    113     }
    114     return 0;
    115 }
    View Code
  • 相关阅读:
    Bootstrap in ASP.NET MVC 5
    Chrome 实用调试技巧
    C#数组
    ASP.NET MVC 表格操作
    jQuery实用Demo
    MySQL学习笔记
    ASP.NET MVC View中的标签(tag)
    ASP.NET MVC HtmlHelper 类的扩展方法
    Delphi按下F1不能出现帮助文档的解决方法
    Delphi函数参数传递 默认参数(传值)、var(穿址)、out(输出)、const(常数)四类
  • 原文地址:https://www.cnblogs.com/jklongint/p/4480797.html
Copyright © 2020-2023  润新知