Submission #64409745


Source Code Expand

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int solve(int M, int K) {
	vector<int> primes;
	for (int i = 2; i <= M; i++) {
		bool f = true;
		for (int j = 2; j * j <= i; j++) {
			if (i % j == 0) {
				f = false;
			}
		}
		if (f) {
			primes.push_back(i);
		}
	}
	int Z = primes.size();
	vector<vector<int> > flag(M + 1, vector<int>(Z, 0));
	for (int i = 1; i <= M; i++) {
		for (int j = 0; j < Z; j++) {
			flag[i][j] = int(i % primes[j] == 0);
		}
	}
	for (int num = M; num >= 1; num--) {
		auto dfs = [&](auto& self, int pos, int cnt, vector<int> cur) -> bool {
			if (cnt > num || cnt + ((M + 1) - pos) < num) {
				return false;
			}
			if (pos == M + 1) {
				return true;
			}
			if (self(self, pos + 1, cnt, cur)) {
				return true;
			}
			for (int i = 0; i < Z; i++) {
				cur[i] += flag[pos][i];
				if (cur[i] >= K) {
					return false;
				}
			}
			if (self(self, pos + 1, cnt + 1, cur)) {
				return true;
			}
			return false;
		};
		if (dfs(dfs, 1, 0, vector<int>(Z, 0))) {
			return num;
		}
	}
	return 0;
}

vector<int> greedy(int M, int K) {
	vector<bool> isprime(M + 1, true);
	isprime[0] = false;
	isprime[1] = false;
	for (int i = 2; i <= M; i++) {
		if (isprime[i]) {
			for (int j = i * 2; j <= M; j += i) {
				isprime[j] = false;
			}
		}
	}
	vector<int> primes;
	for (int i = 2; i <= M; i++) {
		if (isprime[i]) {
			primes.push_back(i);
		}
	}
	int Z = primes.size();
	vector<int> remain(Z, K - 1);
	vector<int> ans = {1};
	for (int i = 0; i < Z; i++) {
		long long x = primes[i];
		while (x <= M && remain[i] != 0) {
			ans.push_back(int(x));
			remain[i]--;
			x *= primes[i];
		}
	}
	for (int i = 0; i < Z - 1; i++) {
		if (1LL * primes[i] * primes[i + 1] <= M) {
			int pos = Z - 1;
			while (pos > i && remain[i] != 0) {
				if (remain[pos] > 0 && 1LL * primes[i] * primes[pos] <= M) {
					long long x = primes[i];
					while (x * primes[pos] <= M && remain[i] != 0 && remain[pos] != 0) {
						long long y = x * primes[pos];
						while (y <= M && remain[i] != 0 && remain[pos] != 0) {
							ans.push_back(int(y));
							remain[i]--;
							remain[pos]--;
							y *= primes[pos];
						}
						x *= primes[i];
					}
				}
				pos--;
			}
		}
	}
	sort(ans.begin(), ans.end());
	return ans;
}

void solver() {
	int M, K;
	cin >> M >> K;
	vector<int> ans = greedy(M, K);
	cout << ans.size() << endl;
	for (int i = 0; i < int(ans.size()); i++) {
		if (i != 0) {
			cout << ' ';
		}
		cout << ans[i];
	}
	cout << endl;
}

int main() {
	solver();
	return 0;
	/*
	for (int M = 1; M <= 100; M++) {
		cout << M << ":";
		for (int K = 2; K <= 5; K++) {
			int res = greedy(M, K).size();
			cout << ' ' << res;
		}
		cout << endl;
	}
	*/
	return 0;
}

Submission Info

Submission Time
Task C - GCD
User square1001
Language C++ 20 (gcc 12.2)
Score 100
Code Size 2852 Byte
Status AC
Exec Time 5 ms
Memory 3804 KiB

Judge Result

Set Name Sample K2 K3 K4 K5
Score / Max Score 0 / 0 10 / 10 20 / 20 30 / 30 40 / 40
Status
AC × 1
AC × 9
AC × 9
AC × 9
AC × 13
Set Name Test Cases
Sample 00_sample_00.txt
K2 01_random_2_00.txt, 01_random_2_01.txt, 01_random_2_02.txt, 01_random_2_03.txt, 01_random_2_04.txt, 02_hand_2_00.txt, 02_hand_2_01.txt, 02_hand_2_02.txt, 02_hand_2_03.txt
K3 03_random_3_00.txt, 03_random_3_01.txt, 03_random_3_02.txt, 03_random_3_03.txt, 03_random_3_04.txt, 04_hand_3_00.txt, 04_hand_3_01.txt, 04_hand_3_02.txt, 04_hand_3_03.txt
K4 05_random_4_00.txt, 05_random_4_01.txt, 05_random_4_02.txt, 05_random_4_03.txt, 05_random_4_04.txt, 06_hand_4_00.txt, 06_hand_4_01.txt, 06_hand_4_02.txt, 06_hand_4_03.txt
K5 00_sample_00.txt, 07_random_5_00.txt, 07_random_5_01.txt, 07_random_5_02.txt, 07_random_5_03.txt, 07_random_5_04.txt, 08_hand_5_00.txt, 08_hand_5_01.txt, 08_hand_5_02.txt, 08_hand_5_03.txt, 08_hand_5_04.txt, 08_hand_5_05.txt, 08_hand_5_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3692 KiB
01_random_2_00.txt AC 2 ms 3612 KiB
01_random_2_01.txt AC 3 ms 3616 KiB
01_random_2_02.txt AC 2 ms 3524 KiB
01_random_2_03.txt AC 3 ms 3640 KiB
01_random_2_04.txt AC 2 ms 3652 KiB
02_hand_2_00.txt AC 1 ms 3488 KiB
02_hand_2_01.txt AC 1 ms 3512 KiB
02_hand_2_02.txt AC 1 ms 3644 KiB
02_hand_2_03.txt AC 4 ms 3652 KiB
03_random_3_00.txt AC 3 ms 3668 KiB
03_random_3_01.txt AC 3 ms 3688 KiB
03_random_3_02.txt AC 4 ms 3656 KiB
03_random_3_03.txt AC 3 ms 3804 KiB
03_random_3_04.txt AC 2 ms 3728 KiB
04_hand_3_00.txt AC 1 ms 3500 KiB
04_hand_3_01.txt AC 1 ms 3488 KiB
04_hand_3_02.txt AC 1 ms 3660 KiB
04_hand_3_03.txt AC 4 ms 3584 KiB
05_random_4_00.txt AC 1 ms 3520 KiB
05_random_4_01.txt AC 3 ms 3668 KiB
05_random_4_02.txt AC 5 ms 3768 KiB
05_random_4_03.txt AC 1 ms 3508 KiB
05_random_4_04.txt AC 5 ms 3644 KiB
06_hand_4_00.txt AC 1 ms 3488 KiB
06_hand_4_01.txt AC 2 ms 3528 KiB
06_hand_4_02.txt AC 1 ms 3572 KiB
06_hand_4_03.txt AC 1 ms 3528 KiB
07_random_5_00.txt AC 3 ms 3616 KiB
07_random_5_01.txt AC 2 ms 3552 KiB
07_random_5_02.txt AC 4 ms 3556 KiB
07_random_5_03.txt AC 3 ms 3612 KiB
07_random_5_04.txt AC 3 ms 3768 KiB
08_hand_5_00.txt AC 1 ms 3616 KiB
08_hand_5_01.txt AC 1 ms 3544 KiB
08_hand_5_02.txt AC 5 ms 3640 KiB
08_hand_5_03.txt AC 1 ms 3524 KiB
08_hand_5_04.txt AC 1 ms 3720 KiB
08_hand_5_05.txt AC 1 ms 3504 KiB
08_hand_5_06.txt AC 1 ms 3420 KiB