結果

問題 No.826 連絡網
ユーザー tkmst201tkmst201
提出日時 2021-02-21 16:51:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 4,535 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 208,508 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 14:50:10
合計ジャッジ時間 3,277 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

namespace tk {
std::vector<std::uint32_t> enumerate_primes(std::uint32_t n) {
	if (n < 2) return {};
	using byte = std::uint8_t;
	using uint32 = std::uint32_t;
	
	constexpr byte m[8] = {1, 7, 11, 13, 17, 19, 23, 29};
	constexpr byte diff[8] = {6, 4, 2, 4, 2, 4, 6, 2}; // [i] := m[i + 1] - m[i]
	constexpr byte plus_byte[8][8] = {
		{0,0,0,0,0,0,0,1},{1,1,1,0,1,1,1,1},{2,2,0,2,0,2,2,1},{3,1,1,2,1,1,3,1},
		{3,3,1,2,1,3,3,1},{4,2,2,2,2,2,4,1},{5,3,1,4,1,3,5,1},{6,4,2,4,2,4,6,1},
	}; // [i][j] := floor(m_i m_{j+1} / 30) - floor(m_i m_j / 30)
	constexpr byte bit_mask[8][8] = {
		{254,253,251,247,239,223,191,127},{253,223,239,254,127,247,251,191},
		{251,239,254,191,253,127,247,223},{247,254,191,223,251,253,127,239},
		{239,127,253,251,223,191,254,247},{223,247,127,253,191,254,239,251},
		{191,251,247,127,254,239,223,253},{127,191,223,239,247,251,253,254},
	}; // [i][j] := ((1<<8)-1) - (1 << to_m_idx(m[i]m[j] (mod. 30))) (mod. 8))
	
	auto pop_count = [](byte x) {
		byte res = (x & 0x55) + (x >> 1 & 0x55);
		res = (res & 0x33) + (res >> 2 & 0x33);
		res = (res + (res >> 4)) & 0xf;
		return res;
	};
	
	auto sqrt = [](uint32 n) {
		uint32 res = 0;
		for (uint32 i = sizeof(n) << 2; i > 0; --i) {
			uint32 x = res + (1u << (i - 1));
			if (x * x <= n) res |= 1u << (i - 1);
		}
		return res;
	};
	
	byte b_idx[129]; // [1 << i] = i
	for (uint32 i = 0; i < 8; ++i) b_idx[1 << i] = i;
	
	auto get_prime_pos = [&](uint32 n) {
		uint32 s = sqrt(n); // floor(sqrt(n))
		const uint32 s30 = s / 30 + (s % 30 != 0);
		const uint32 n30 = n / 30 + (n % 30 != 0);
		
		std::vector<byte> isprime(n30, 255);
		isprime[0] = 254;
		for (uint32 i = 0; i < s30; ++i) {
			for (byte j = isprime[i]; j; j &= j - 1) {
				const byte mi = b_idx[j & -j];
				byte k = mi;
				for (uint32 b = (30*i + 2*m[mi])*i + m[mi]*m[mi]/30; b < n30; b += i*diff[k] + plus_byte[mi][k], k = (k + 1) & 7)
					isprime[b] &= bit_mask[mi][k];
			}
		}
		
		for (uint32 i = 8; i > 0; --i) {
			if ((n30 - 1) * 30 + m[i - 1] <= n) break;
			isprime.back() &= ~(1 << (i - 1));
		}
		
		uint32 cnt = 0;
		std::vector<uint32> prime_pos; // {byte} << 3 | {bit}
		for (uint32 i = 0; i < n30; ++i) {
			for (uint32 j = isprime[i]; j; j &= j - 1) {
				prime_pos.emplace_back(i << 3 | b_idx[j & -j]);
			}
		}
		return prime_pos;
	};
	
	auto prime_pos = get_prime_pos(sqrt(n));
	constexpr uint32 segment = 1 << 18;
	std::vector<uint32> pos;
	pos.reserve(prime_pos.size());
	for (uint32 i = 0; i < prime_pos.size(); ++i) {
		const uint32 pb = prime_pos[i] >> 3;
		const byte mi = prime_pos[i] & 7;
		pos.emplace_back(((30*pb + 2*m[mi])*pb + m[mi]*m[mi]/30) << 3 | mi);
	}
	
	
	const uint32 n30 = n / 30 + (n % 30 != 0);
	
	std::vector<uint32> primes;
	if (2 <= n) primes.emplace_back(2);
	if (3 <= n) primes.emplace_back(3);
	if (5 <= n) primes.emplace_back(5);
	
	std::vector<uint32> isprime;
	for (uint32 l = 0; l < n30; l += segment) {
		const uint32 d = std::min(segment, n30 - l);
		const uint32 r = l + d;
		
		isprime.assign(d, 255);
		if (l == 0) isprime[0] = 254;
		
		for (uint32 i = 0; i < prime_pos.size(); ++i) {
			const uint32 pb = prime_pos[i] >> 3;
			const byte mi = prime_pos[i] & 7;
			uint32 b = pos[i] >> 3;
			byte k = pos[i] & 7;
			while (b < d) {
				isprime[b] &= bit_mask[mi][k];
				b += pb*diff[k] + plus_byte[mi][k];
				k = (k + 1) & 7;
			}
			pos[i] = (b - d) << 3 | k;
		}
		
		for (uint32 i = 8; i > 0; --i) {
			if ((r - 1) * 30 + m[i - 1] <= n) break;
			isprime.back() &= ~(1 << (i - 1));
		}
		
		for (uint32 i = 0; i < d; ++i) {
			for (uint32 j = isprime[i]; j; j &= j - 1) primes.emplace_back((l + i) * 30 + m[b_idx[j & -j]]);
		}
	}
	return primes;
}
} // namespace tk

int main() {
	int N, P;
	cin >> N >> P;
	auto prime = tk::enumerate_primes(N);
	int cnt = 1;
	bool in = false;
	for (int p : prime) {
		in |= 2 * p <= N && P % p == 0;
		cnt += 2 * p > N;
	}
	int ans = in ? cnt : 1;
	cout << (in ? N - cnt : 1) << endl;
}
0