其他分享
首页 > 其他分享> > 博弈论-nim博弈2(hh2048题单)

博弈论-nim博弈2(hh2048题单)

作者:互联网

【题目大意】:

  简单nim  输出第一步的方案数.

【思路】:

  nim部分是普通的nim , 难的是输出第一步的方案数。

  假设x 是 异或 后的值 。x = a1 ^a2 ^ a3 ^ a4 ^ a5 。

  第一步的想法是让剩余的数异或后x = 0 。

  如果 存在 ai > (ai^x) // 位运算优先级最低 所以要括起来.

  那么 ai - (ai^x )< ai

  那么就存在 ai - (ai-(ai^x))  == ai^x;

  就相当于  原先的a1^a2^a3^a4^a5^......^an = x;

  后来减去之后     a1^a2^a3^a4^....^(ai - (ai-(ai^x) ) ^...an  = a1^a2^a3^a4^...^ai^x^...^an = x^x = 0;

  所以只需要找到 ai > (ai^x ) 。

【题解】:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>

#define ms(a, b) memset(a,b,sizeof(a))
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define rep(i, a, b)  for(ll i=a;i<=b;i++)
#define lep(i, a, b)  for(ll i=a;i>=b;i--)
#define endl '\n'
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi  vector<ll>
#define vpi vector<pii>
#define vpl vector<pll>
#define mi  map<ll,ll>
#define all(a)  (a).begin(),(a).end()
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

#define ff first
#define ss second
#define test4(x, y, z, a) cout<<"x is "<<x<<"        y is "<<y<<"        z is "<<z<<"        a is "<<a<<endl;
#define test3(x, y, z) cout<<"x is "<<x<<"        y is "<<y<<"        z is "<<z<<endl;
#define test2(x, y) cout<<"x is "<<x<<"        y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
using namespace std;
const int N =1000;
const int maxx = 0x3f3f3f;
const int mod = 1e9 + 7;
const int minn = -0x3f3f3f;
const int M = 2 * N;
ll T, n, m;

void solve() {
    int res = 0 , a [N] ;
    ms(a,0);
    rep(i,1,n) {
        cin>>a[i];
        res ^= a[i]  ;
    }
//    cout<<res<<endl;
    int ans = 0;
    if ( res == 0 ) cout<<"0"<<endl;
    else {
        rep(i,1,n){
            if ( a[i] > (a[i]^res) ){
                ans ++ ;
//                int z = a[i] ^ res ;
//                test3(a[i],res,z);
            }
        }
        cout<<ans<<endl;
    }
}

int main() {
    fast;
    while (1) {
        cin>>n;
        if ( n==0 ) return 0;
        solve();
    }
}
View Code

 

标签:cout,nim,ai,res,题单,long,include,hh2048,define
来源: https://www.cnblogs.com/opanc/p/16118189.html