結果

問題 No.2487 Multiple of M
ユーザー 259-Momone259-Momone
提出日時 2023-09-29 22:04:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,311 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 267,364 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 12:51:22
合計ジャッジ時間 4,732 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 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,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 1 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/modint>
#include <bits/extc++.h>

int main() {
    using namespace std;
    // using modint = atcoder::static_modint<998244353>;
    unsigned long N, M, K;
    cin >> N >> M >> K;
    auto g{gcd(M, K)};
    vector<unsigned long> prime_factors;
    for (unsigned long p{2}; p * p <= g; ++p)
        if (g % p == 0) {
            prime_factors.emplace_back(p);
            while (g % p == 0)
                g /= p;
        }
    if(g > 1)prime_factors.emplace_back(g);
    
    const auto P{size(prime_factors)};
    auto M_p{M};
    vector<unsigned long> counter(60, 1);
    for (unsigned long i{}; i < P; ++i) {
        const auto p{prime_factors[i]};
        unsigned long x_M{1}, x_K{1};
        while (M_p % p == 0) {
            M_p /= p;
            x_M *= p;
        }
        while (K % (x_K * p) == 0)
            x_K *= p;
        unsigned long x{1};
        for (unsigned long j{}; j < 60; ++j) {
            counter[j] *= x;
            x = min(x_M, x * x_K);
        }
    }

    for (unsigned long j{60}; --j;)
        counter[j] -= counter[j - 1];

    while (counter.back() == 0)
        counter.pop_back();

    counter.emplace_back(M / M_p * (M_p - 1));

    const auto C{size(counter)};

    vector matrix(C, vector<unsigned long>(C));
    for (unsigned long i{}; i < C; ++i) {
        for (unsigned long j{}; j < C; ++j)
            matrix[i][j] = counter[j];
        matrix[i][i - (i && i + 1 < C)] -= 1;
    }

    vector ans(C, vector<unsigned long>(C));
    for (unsigned long i{}; i < C; ++i)
        ans[i][i] = 1;

    const auto multiply{
        [C](const auto &lhs, const auto &rhs) {
            vector ret(C, vector<unsigned long>(C));
            for (unsigned long i{}; i < C; ++i) {
                for (unsigned long j{}; j < C; ++j) {
                    for (unsigned long k{}; k < C; ++k) {
                        ret[i][k] += lhs[i][j] * rhs[j][k] % 998244353;
                    }
                }
            }
            for (auto &&row : ret)
                for (auto &&x : row)
                    x %= 998244353;
            return ret;
        }};

    while (N) {
        if (N & 1)
            ans = multiply(ans, matrix);
        matrix = multiply(matrix, matrix);
        N /= 2;
    }

    cout << ans[0][0] << endl;
    return 0;
}
0