洛谷 [P2953] 牛的数字游戏

SG搜索

n的范围在可以接受的范围内,SG搜索即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1000005;
int init() {
	int rv = 0 , fh = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') fh = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		rv = (rv<<1) + (rv<<3) + c - '0';
		c = getchar();
	}
	return fh * rv;
}
int SG[MAXN], T, n;
int SG_search(int cur) {
	if(SG[cur] != -1) return SG[cur];
	int t = cur, ma = 0, mi = 9;
	while(t) {
		int k = t % 10;
		if(k){
			mi = min(mi, k);
			ma = max(ma, k);
		}
		t /= 10;
	}
	int r1 = SG_search(cur - ma), r2 = SG_search(cur - mi);
	if(min(r1, r2)) return SG[cur] = 0;
	else if(max(r1, r2) == 1) return SG[cur] = 2;
	else return SG[cur] = 1;
}
int main() {
	freopen("in.txt", "r", stdin);
	T = init();
	memset(SG, -1, sizeof(SG));
	SG[0] = 0;
	for(int i = 1 ; i <= MAXN - 5 ; i++) {
		SG_search(i);
	}
	while(T--) {
		n = init();
		if(SG[n]) printf("YES\n");
		else printf("NO\n");
	}
	fclose(stdin);
	return 0;
}
posted @ 2018-02-28 16:43  Mr_Wolfram  阅读(199)  评论(0编辑  收藏  举报