結果

問題 No.1396 Giri
ユーザー merom686merom686
提出日時 2021-02-14 21:57:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,122 bytes
コンパイル時間 2,843 ms
コンパイル使用メモリ 92,264 KB
実行使用メモリ 77,948 KB
最終ジャッジ日時 2023-09-29 15:25:19
合計ジャッジ時間 3,140 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 102 ms
77,948 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 103 ms
77,540 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 9 ms
8,952 KB
testcase_19 AC 51 ms
42,032 KB
testcase_20 AC 70 ms
56,824 KB
testcase_21 AC 91 ms
70,224 KB
testcase_22 AC 106 ms
76,676 KB
testcase_23 AC 103 ms
77,588 KB
testcase_24 AC 103 ms
77,520 KB
testcase_25 AC 103 ms
77,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

constexpr int P = 998244353;

ll powmod(ll n, ll k) {
    ll r = 1, t = n % P;
    for (; k != 0; k /= 2) {
        if (k & 1) r = r * t % P;
        t = t * t % P;
    }
    return r;
}
ll modinv(ll n) {
    return powmod(n, P - 2);
}

struct Sieve {
    Sieve(int n) : l(n + 1, 0) {
        for (int i = 2; i <= n; i++) {
            if (!l[i]) p.push_back(l[i] = i);
            for (int q : p) {
                if (q > l[i]) break;
                if (int j = i * q; j <= n) l[j] = q; else break;
            }
        }
    }
    bool is_prime(int i) {
        return l[i] == i;
    }
    vector<int> l, p;
};

int main() {
    int n;
    cin >> n;

    Sieve si(n);

    vector<array<int, 2>> x(n + 1);
    vector<array<array<int, 2>, 8>> y(n + 1);

    for (int i = 1; i <= n; i++) {
        int k = 0, l = 0, m = 0;
        for (int t = i; t != 1;) {
            int p = si.l[t];
            t /= p;
            if (p != l) {
                if (k) y[i][m++] = { l, k };
                k = 1;
                l = p;
            } else {
                k++;
            }
        }
        if (k) y[i][m++] = { l, k };

        for (int j = 0; j < m; j++) {
            auto [l, k] = y[i][j];
            if (k > x[l][1]) {
                if (k > x[l][0]) {
                    x[l][1] = x[l][0];
                    x[l][0] = k;
                } else {
                    x[l][1] = k;
                }
            }
        }
    }

    ll a0 = 1, a1 = 1;
    for (int i = 1; i <= n; i++) {
        if (x[i][0]) a0 = a0 * powmod(i, x[i][0]) % P;
    }
    for (int i = 1; i <= n; i++) {
        ll t = 1;
        for (int j = 0; j < 8; j++) {
            auto [l, k] = y[i][j];
            if (!k) break;
            if (k == x[l][0]) {
                t *= powmod(l, x[l][0] - x[l][1]);
            }
        }
        a1 = max(a1, t);
    }

    ll r = a0 * modinv(a1) % P;
    cout << r << endl;

    return 0;
}
0