結果

問題 No.106 素数が嫌い!2
ユーザー masamasa
提出日時 2015-04-26 21:50:48
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,201 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 66,508 KB
実行使用メモリ 11,572 KB
最終ジャッジ日時 2023-09-18 11:51:41
合計ジャッジ時間 13,210 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

const int MAX_N = 2e6;

vector<int> getPrimes(int n) {
	vector<bool> isPrime(n+1, true);

	isPrime[0] = isPrime[1] = false;

	for (int i = 2; i * i <= n; i++) {
		if (isPrime[i]) {
			for (int j = i * 2; j <= n; j += i) {
				isPrime[j] = false;
			}
		}
	}

	vector<int> primes(n + 1, 0);
	int idx = 0;
	for (int i = 0; i <= n; i++) {
		if (isPrime[i]) {
			primes[idx] = i;
			idx++;
		}
	}

	primes.erase(primes.begin() + idx, primes.end());
	return primes;
}

int main() {
	int n, k;

	cin >> n >> k;
	auto primes = getPrimes(n);
	int np = primes.size();

	long long mini = 1;
	for (int i = 0; i < k; i++) {
		mini *= (long long) primes[i];
		if (mini > MAX_N || mini > n) {
			cout << 0 << endl;
			return 0;
		}
	}

	int ans = 1; // miniはOK
	for (int i = mini + 1; i <= n; i++) {
		int num = i;
		int n_use = 0;
		for (int j = 0; j < np; j++) {
			bool used = false;
			while (num % primes[j] == 0) {
				used = true;
				num /= primes[j];
			}

			if (used) {
				n_use++;
				if (n_use >= k) {
					ans++;
					break;
				}
			}
		}
	}

	cout << ans << endl;
	return 0;
}
0