跳到正文

题解

题解 P5635 【【CSGRound1】天下第一】

Eason_AC约 1 分钟
原文
文章信息与版本

文章信息

发布
2019年11月11日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟
获赞
12
题目
P5635

版本

  1. v1当前

标签里面说要记忆化搜索?

个人感觉其实没有必要,只需要稍微带一点小技巧的搜索就可以过了。


因为不能决出胜负的局肯定到最后会进入死循环,所以我们可以限制搜索的层数(就是说达到一定的层数就干脆不搜了,直接跳出搜索),然后就是平常地搜索就行了。

Code 100pts\text{Code 100pts}:

C++
#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;
}
原文
存档评论3 条抓取于