結果

問題 No.1396 Giri
ユーザー tkmst201tkmst201
提出日時 2021-02-15 19:17:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 7,485 bytes
コンパイル時間 2,405 ms
コンパイル使用メモリ 209,488 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-23 10:20:55
合計ジャッジ時間 3,342 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 4 ms
6,940 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 = 998244353;
constexpr bool debug = false;
//---------------------------------//

template<int M>
struct ModInt {
	static_assert(M > 0);
	
public:
	using value_type = int;
	using calc_type = std::int_fast64_t;
	
private:
	value_type val_;
	
public:
	constexpr ModInt(calc_type val = 0) : val_(val < 0 ? (val % M + M) % M : val % M) {}
	constexpr const value_type & val() const noexcept { return val_; }
	constexpr static decltype(M) mod() noexcept { return M; }
	
	explicit constexpr operator bool() const noexcept { return val_; }
	constexpr bool operator !() const noexcept { return !static_cast<bool>(*this); }
	constexpr ModInt operator +() const noexcept { return ModInt(*this); }
	constexpr ModInt operator -() const noexcept { return ModInt(-val_); }
	constexpr ModInt operator ++(int) noexcept { ModInt res = *this; ++*this; return res; }
	constexpr ModInt operator --(int) noexcept { ModInt res = *this; --*this; return res; }
	constexpr ModInt & operator ++() noexcept { val_ = val_ + 1 == M ? 0 : val_ + 1; return *this; }
	constexpr ModInt & operator --() noexcept { val_ = val_ == 0 ? M - 1 : val_ - 1; return *this; }
	constexpr ModInt & operator +=(const ModInt & rhs) noexcept { val_ += val_ < M - rhs.val_ ? rhs.val_ : rhs.val_ - M; return *this; }
	constexpr ModInt & operator -=(const ModInt & rhs) noexcept { val_ += val_ >= rhs.val_ ? -rhs.val_ : M - rhs.val_; return *this; }
	constexpr ModInt & operator *=(const ModInt & rhs) noexcept { val_ = static_cast<calc_type>(val_) * rhs.val_ % M; return *this; }
	constexpr ModInt & operator /=(const ModInt & rhs) noexcept { return *this *= rhs.inv(); }
	friend constexpr ModInt operator +(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) += rhs; }
	friend constexpr ModInt operator -(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) -= rhs; }
	friend constexpr ModInt operator *(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) *= rhs; }
	friend constexpr ModInt operator /(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) /= rhs; }
	friend constexpr bool operator ==(const ModInt & lhs, const ModInt & rhs) noexcept { return lhs.val_ == rhs.val_; }
	friend constexpr bool operator !=(const ModInt & lhs, const ModInt & rhs) noexcept { return !(lhs == rhs); }
	friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val_; }
	friend std::istream & operator >>(std::istream & is, ModInt & rhs) { calc_type x; is >> x; rhs = ModInt(x); return is; }
	
	constexpr ModInt pow(calc_type n) const noexcept {
		ModInt res = 1, x = val_;
		if (n < 0) { x = x.inv(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	constexpr ModInt inv() const noexcept {
		value_type a = val_, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
		while (b > 0) {
			value_type q = a / b, r = a % b;
			value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
			a = b; b = r;
			a1 = b1; b1 = nb1;
			a2 = b2; b2 = nb2;
		}
		assert(a == 1);
		return a1;
	}
};
using mint = ModInt<MOD>;

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);
	}
	
	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);
	
	const uint32 n30 = n / 30 + (n % 30 != 0);
	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;
	cin >> N;
	
	auto prime = tk::enumerate_primes(N);
	prime.pop_back();
	
	mint ans = 1;
	for (int p : prime) {
		int s = p, c = 1;
		while ((ll)s * p <= N) s *= p;
		ans *= s;
	}
	
	cout << ans << endl;
}
0