求大佬指指点点,用的是并查集原理,只能90分
查看原帖
求大佬指指点点,用的是并查集原理,只能90分
892762
Itismyname楼主2023/4/20 17:01
#define _CRT_SECURE_NO_WARNINGS 1
#define ll long long
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<functional>
#include<numeric>

using namespace std;

int n, m, c = 1, now = 0, x = 0;
int used[300], pre[300];

int find(int x)
{
	return x == pre[x] ? x : pre[x] = find(pre[x]);
}

int main()
{
	cin >> n >> m;

	for (int i = 0; i <= n; ++i)pre[i] = i;
	used[0] = 1;
	
	while (now < m)
	{
		if (x > n)
		{
			x = 0;
			continue;
		}

		x = find(x);

		if (!used[x])
		{
			used[x] = 1;
			pre[x] = x + 1;
			if (++now == m)
			{
				cout << x;
				break;
			}
		}

		x += (c * c * c) % 5 + 2;
		++c;
	}

	return 0;
}
2023/4/20 17:01
加载中...