1. 程式人生 > 其它 >LuoguP5635 【CSGRound1】天下第一 題解

LuoguP5635 【CSGRound1】天下第一 題解

LuoguP5635 【CSGRound1】天下第一 題解

標籤裡面說要記憶化搜尋?

個人感覺其實沒有必要,只需要稍微帶一點小技巧的搜尋就可以過了。


因為不能決出勝負的局肯定到最後會進入死迴圈,所以我們可以限制搜尋的層數(就是說達到一定的層數就乾脆不搜了,直接跳出搜尋),然後就是平常地搜尋就行了。

\(\text{Code 100pts}\)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;

int x, y, mod, t;
int dfs(int x, int y, int dep) {
	if(dep > 10000)	return 0;
	if(!x)	return 1;
	if(!y)	return 2;
//	printf("%d\n", dep); 
	if(dep % 2)	return dfs((x + y) % mod, y, dep + 1);
	return dfs(x, (x + y) % mod, dep + 1);
}

int main() {
	scanf("%d%d", &t, &mod);
	while(t--) {
		scanf("%d%d", &x, &y);
		if(!dfs(x, y, 1))	puts("error");
		else printf("%d\n", dfs(x, y, 1));
	}
	return 0;
}