CF1033C Permutation Game(博弈论+拓扑)

题目传送

当无法移动时,为必败态,至少有一种方法可以到达必败态的状态为必胜态,无论怎么走都是必胜态的状态为必败态。
棋子可以从数值小的格子移向数值大的格子,从当前格向可到达的格子连有向边,可形成一个有向无环图,格子中的数字就是拓扑序,按拓扑序从大到小遍历,每次遍历到距当前格长度为当前格数值倍数的格子,如果到达的格为必败态,则当前格就是必胜态,到达的所有格都是必胜态,当前格就为必败态。

详见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
const ll Inf = 1e18;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
	return f * x;
}

int an[maxn];
int pos[maxn];
int state[maxn];

int main(void)
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> an[i];
		pos[an[i]] = i; //数值的位置
	}
	for (int i = n; i >= 1; i--) {
		int u = pos[i];
		state[u] = 0; //初始化为必败态
		int v = u;
		while (v - an[u] > 0) v -= an[u];
		for (; v <= n; v += an[u]) {
			//可以到达一种必败态
			if (an[v] > an[u] && state[v] == 0) {
				state[u] = 1; //当前格为必胜态
				break;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (state[i]) cout << "A";
		else cout << "B";
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值