結果

問題 No.1396 Giri
ユーザー mine691mine691
提出日時 2021-02-14 21:43:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 647 ms / 2,000 ms
コード長 2,102 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 210,200 KB
最終ジャッジ日時 2025-01-18 20:50:01
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
constexpr static int mod = 1e9 + 7;
constexpr static int inf = (1 << 30) - 1;
constexpr static Int infll = (1LL << 61) - 1;
int Competitive_Programming = (ios_base::sync_with_stdio(false), cin.tie(nullptr), cout << fixed << setprecision(15), 0);
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

//N以下の数について、それぞれの素因数を重複を除いて求める
vector<vector<int>> prime_factor_lists(int N)
{
	vector<vector<int>> prime_lists(N + 1);
	for (int i = 2; i <= N; i++)
	{
		if (prime_lists[i].size() == 0)
		{
			for (int j = i; j <= N; j += i)
			{
				prime_lists[j].push_back(i);
			}
		}
	}
	return prime_lists;
}

// for query using osa_k way

// init table
vector<int> sieve(int N)
{
	vector<int> res(N + 1);

	for (int i = 2; i <= N; i++)
	{
		if (res[i] == 0)
		{
			for (int j = i; j <= N; j += i)
			{
				if (res[j] == 0)
					res[j] = i;
			}
		}
	}
	return res;
}

// prime factorization O(log N)
vector<int> prime_factor(int N, const vector<int> &min_factor)
{
	// min_factor is vector which got by sieve()
	vector<int> res;
	while (N > 1)
	{
		res.push_back(min_factor[N]);
		N /= min_factor[N];
	}
	return res;
}

bool is_prime(int N, const vector<int> &min_factor)
{
	return min_factor[N] == N;
}

using ll = long long;

ll mod_pow(ll x, ll n, ll mod)
{
	ll res = 1;
	while (n > 0)
	{
		if (n & 1)
			(res *= x) %= mod;
		(x *= x) %= mod;
		n >>= 1;
	}
	return res;
}

int main()
{
	int N;
	cin >> N;
	auto mi = sieve(N + 1);
	int ex = 0;
	for (int i = N; i > 1; i--)
	{
		if (is_prime(i, mi))
		{
			ex = i;
			break;
		}
	}

	map<int, int> mp;
	mp[1]++;

	for (int i = 2; i <= N; i++)
	{
		if (i == ex)
			continue;
		auto p = prime_factor(i, mi);
		map<int, int> q;
		for (int j = 0; j < p.size(); j++)
		{
			q[p[j]]++;
		}

		for (auto &&e : q)
			mp[e.first] = max(mp[e.first], e.second);
	}

	Int ans = 1;

	for (auto &&e : mp)
	{
		ans *= mod_pow((Int)e.first, (Int)e.second, 998244353);
		ans %= 998244353;
	}

	cout << ans << '\n';
}
0