結果

問題 No.181 A↑↑N mod M
ユーザー 👑 NachiaNachia
提出日時 2020-10-22 20:54:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,121 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 165,124 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 14:51:53
合計ジャッジ時間 3,984 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,376 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 WA -
testcase_35 AC 1 ms
4,376 KB
testcase_36 WA -
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 WA -
testcase_42 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

ULL A, N, M;
ULL powA[2001];
ULL cys, cyt;
ULL P[30][2001];

ULL applycycle(ULL idx) {
    if (idx < cys) return idx;
    ULL cyz = cyt - cys;
    return (idx - cys) % cyz + cys;
}

ULL powapplycycle(ULL a, ULL i) {
    if (i == 0) return applycycle(1);
    ULL res = powapplycycle(applycycle(a * a), i / 2);
    if (i % 2 == 1) res = applycycle(res * a);
    return res;
}

int main() {
    cin >> A >> N >> M;
    powA[0] = 1 % M; rep(i, M) powA[i + 1] = powA[i] * A % M;
    cys = cyt = ~0ull;
    rep(i, M + 1) {
        for (int j = i + 1; j <= M; j++) if (powA[i] == powA[j]) { cyt = j; break; }
        if (cyt != ~0ull) { cys = i; break; }
    }

    rep(i, cyt) P[0][i] = powapplycycle(A, i);
    rep(d, 29) rep(i, cyt) P[d + 1][i] = P[d][P[d][i]];

    ULL ans;

    if (N == 0) ans = 1;
    else {
        N--;
        ans = applycycle(1);
        rep(d, 30) if (N & (1 << d)) ans = P[d][ans];
        ans = powA[ans];
    }

    cout << ans << endl;

    return 0;
}
0