博弈論-nim博弈2(hh2048題單)
阿新 • • 發佈:2022-04-08
【題目大意】:
簡單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> #includeView Code<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> #definepll 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(); } }