結果

問題 No.1529 Constant Lcm
ユーザー rianoriano
提出日時 2021-06-04 20:17:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,116 ms / 3,000 ms
コード長 2,657 bytes
コンパイル時間 1,897 ms
コンパイル使用メモリ 186,912 KB
実行使用メモリ 15,716 KB
最終ジャッジ日時 2023-08-12 09:01:51
合計ジャッジ時間 14,187 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 989 ms
14,404 KB
testcase_11 AC 353 ms
7,968 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 826 ms
13,152 KB
testcase_14 AC 478 ms
9,536 KB
testcase_15 AC 545 ms
9,840 KB
testcase_16 AC 421 ms
8,764 KB
testcase_17 AC 209 ms
6,336 KB
testcase_18 AC 230 ms
6,468 KB
testcase_19 AC 517 ms
10,032 KB
testcase_20 AC 1,104 ms
15,716 KB
testcase_21 AC 1,089 ms
15,636 KB
testcase_22 AC 1,076 ms
15,584 KB
testcase_23 AC 1,116 ms
15,652 KB
testcase_24 AC 1,087 ms
15,656 KB
testcase_25 AC 1,045 ms
15,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0