結果
問題 | No.1529 Constant Lcm |
ユーザー |
![]() |
提出日時 | 2021-06-04 20:17:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,122 ms / 3,000 ms |
コード長 | 2,657 bytes |
コンパイル時間 | 2,337 ms |
コンパイル使用メモリ | 188,424 KB |
実行使用メモリ | 15,884 KB |
最終ジャッジ日時 | 2024-11-19 08:28:04 |
合計ジャッジ時間 | 14,315 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 24 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define ll long long#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)#define Pr pair<ll,ll>#define Tp tuple<int,int,int>using Graph = vector<vector<int>>;ll mod = 998244353;//素数判定// nまでの整数に最小の素因数minfactorを与えるvector<int> min_factor;vector<int> sieve(int n) {vector<int> res(n+1);iota(res.begin(), res.end(), 0);for (int i = 2; i*i <= n; ++i) {if (res[i] < i) continue;for (int j = i*i; j <= n; j += i) {if (res[j] == j) res[j] = i;}}return res;}// nの素因数分解(上の"sieve"を前提とする)// (p1,n1),(p2,n2),...vector<pair<int,int>> factor(int n) {// min_factor は sieve() で得られたものとするvector<pair<int,int>> res; int p=-1,cnt=0;while (n > 1) {if(min_factor[n]!=p&&cnt>0){res.push_back(make_pair(p,cnt));p = min_factor[n]; cnt = 1;}else if(min_factor[n]!=p){p = min_factor[n]; cnt = 1;}else cnt++;n /= min_factor[n];// 割った後の値についても素因数を知っているので順次求まる}if(p!=-1) res.push_back(make_pair(p,cnt));return res;}//累乗 aのb乗、正しmを法として求めるlong long pw(long long a,long long b,long long m){if(b==0) return 1;else if(b%2==0){long long x = pw(a,b/2,m);return (x*x)%m;}else{long long x = pw(a,b-1,m);return (a*x)%m;}}int main() {ll N; cin >> N;//main関数内min_factor = sieve(N); //nまでの素数判定set<ll> pr;map<ll,ll> cnt;rep(i,N/2){vector<pair<int,int>> primefac = factor(i+1); //kの素因数分解set<ll> pr1;map<ll,ll> cnt1;for(auto p:primefac){int x = p.first; int y = p.second; //素因数xはy個pr1.insert(x);if(cnt1.count(x)) cnt1[x] += y;else cnt1[x] = y;}primefac = factor(N-1-i);for(auto p:primefac){int x = p.first; int y = p.second; //素因数xはy個pr1.insert(x);if(cnt1.count(x)) cnt1[x] += y;else cnt1[x] = y;}for(ll x:pr1){if(pr.count(x)){cnt[x] = max(cnt[x],cnt1[x]);}else{cnt[x] = cnt1[x];pr.insert(x);}}}ll ans = 1;for(ll x:pr){ans *= pw((ll)x,(ll)cnt[x],mod);ans %= mod;}cout << ans << endl;}