【UR #4】元旦三侠的游戏(博弈论+记忆化)

http://uoj.ac/contest/6/problem/51

题意:给m($m \le 10^5$)个询问,每次给出$a, b(a^b \le n, n \le 10^9)$,对于每一组$a, b$,双人博弈,每次可以给$a$加1或给$b$加1,要求每次操作后$a^b \le n$。不能操作的算输。问先手是否必胜。

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i=0; i<(n); ++i)

#define for1(i,a,n) for(int i=(a);i<=(n);++i)

#define for2(i,a,n) for(int i=(a);i<(n);++i)

#define for3(i,a,n) for(int i=(a);i>=(n);--i)

#define for4(i,a,n) for(int i=(a);i>(n);--i)

#define CC(i,a) memset(i,a,sizeof(i))

#define read(a) a=getint()

#define print(a) printf("%d", a)

#define dbg(x) cout << (#x) << " = " << (x) << endl

#define error(x) (!(x)?puts("error"):0)

#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)

inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }



const int N=1e5+10;

int f[N][33], mx[N];

ll n;



int ask(int a, int b) {

	if((ll)a*a>n) { if(b>1) return 0; return !((n-a)&1); }

	if(b>mx[a]) return 0;

	if(f[a][b]!=-1) return f[a][b];

	if(!ask(a+1, b) && !ask(a, b+1)) return f[a][b]=1;

	return f[a][b]=0;

}

void init() {

	int sq=sqrt(n+0.5);

	for1(i, 2, sq) { 

		int cnt=1; ll now=i;

		while(now*i<=n) {

			++cnt;

			now*=i;

		} //dbg(cnt);

		mx[i]=cnt; 

	}

}

int main() {

	read(n);

	CC(f, -1);

	int m=getint();

	init();

	while(m--) {

		int a=getint(), b=getint();

		!ask(a, b)?puts("Yes"):puts("No");

	}

	return 0;

}

 

显然指数大于等于2的底数小于等于$sqrt(n)$,当底数大于了$sqrt(n)$我们能够根据奇偶判断胜负

然后有$sqrt(n)$个底数,每个底数最多不超过$log n=31$,所以直接记忆化暴力...复杂度$O(sqrt(n)logn)$

你可能感兴趣的:(游戏)