1. 程式人生 > >Nowcoder 挑戰賽23 B 遊戲 ( NIM博弈、SG函數打表 )

Nowcoder 挑戰賽23 B 遊戲 ( NIM博弈、SG函數打表 )

inline tar ack ddd long err push_back ron iop

題目鏈接

題意 : 中文題、點鏈接

分析 :

前置技能是 SG 函數、NIM博弈變形

每次可取石子是約數的情況下、那麽就要打出 SG 函數

才可以去通過異或操作判斷一個局面的勝負

打 SG 函數的時候、由於 N 很大

所以不能使用遞歸的方式打表、會爆棧

還有要預處理每個數的約數

打出 SG 函數之後

暴力判斷初始局面的每堆石子取走約數後是否對答案產生貢獻

技術分享圖片
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scl(i) scanf("%lld", &i)
#define
scll(i, j) scanf("%lld %lld", &i, &j) #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k) #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l) #define scs(i) scanf("%s", i) #define sci(i) scanf("%d", &i) #define scd(i) scanf("%lf", &i) #define
scIl(i) scanf("%I64d", &i) #define scii(i, j) scanf("%d %d", &i, &j) #define scdd(i, j) scanf("%lf %lf", &i, &j) #define scIll(i, j) scanf("%I64d %I64d", &i, &j) #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k) #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k) #define
scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k) #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l) #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l) #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define lowbit(i) (i & (-i)) #define mem(i, j) memset(i, j, sizeof(i)) #define fir first #define sec second #define VI vector<int> #define ins(i) insert(i) #define pb(i) push_back(i) #define pii pair<int, int> #define VL vector<long long> #define mk(i, j) make_pair(i, j) #define all(i) i.begin(), i.end() #define pll pair<long long, long long> #define _TIME 0 #define _INPUT 0 #define _OUTPUT 0 clock_t START, END; void __stTIME(); void __enTIME(); void __IOPUT(); using namespace std; const int maxn = 1e5 + 10; int SG[maxn]; VI divisor[maxn]; inline void init() { for(int i=1; i<maxn; i++) for(int j=i; j<maxn; j+=i) divisor[j].pb(i); SG[0] = 0; for(int i=1; i<maxn; i++){ set<int> s; for(int j=0; j<(int)divisor[i].size(); j++) s.ins(SG[i-divisor[i][j]]); for(int j=0;;j++) if(!s.count(j)){ SG[i] = j; break; } } } int arr[maxn]; int main(void){__stTIME();__IOPUT(); init(); int n; sci(n); int xorSum = 0; for(int i=0; i<n; i++) sci(arr[i]), xorSum ^= SG[arr[i]]; int ans = 0; for(int i=0; i<n; i++){ for(int j=0; j<(int)divisor[arr[i]].size(); j++){ if((xorSum ^ SG[arr[i]] ^ SG[arr[i] - divisor[arr[i]][j]]) == 0) ans++; } } printf("%d\n", ans); __enTIME();return 0;} void __stTIME() { #if _TIME START = clock(); #endif } void __enTIME() { #if _TIME END = clock(); cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl; #endif } void __IOPUT() { #if _INPUT freopen("in.txt", "r", stdin); #endif #if _OUTPUT freopen("out.txt", "w", stdout); #endif }
View Code

Nowcoder 挑戰賽23 B 遊戲 ( NIM博弈、SG函數打表 )