結果

問題 No.181 A↑↑N mod M
ユーザー mayoko_mayoko_
提出日時 2015-04-06 19:49:19
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,483 bytes
コンパイル時間 1,140 ms
コンパイル使用メモリ 144,932 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 06:18:09
合計ジャッジ時間 2,704 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

const int MAXN = 3000;
int phi[MAXN];

void initPhi() {
    for (int i = 0; i < MAXN; i++) phi[i] = i;
    for (int i = 2; i < MAXN; i++) {
        if (i == phi[i]) {
            for (int j = 1; i*j < MAXN; j++) {
                phi[i*j] -= phi[i*j]/i;
            }
        }
    }
}

ll check(ll A, ll N) {
    if (A==1) return 1;
    ll ret = 1;
    for (int i = 0; i < N; i++) {
        ll tmp = ret;
        ret = 1;
        for (int j = 0; j < tmp; j++) {
            ret *= A;
            if (ret > 3000) return 3000;
        }
    }
    return ret;
}

// a^p mod mを求める
ll modpow(ll a, ll p, ll m) {
    if (a == 0) return 0;
    if (p == 0) return 1;
    if (p%2 == 0) {
        ll tmp = modpow(a, p/2, m);
        return tmp*tmp % m;
    } else {
        return a*modpow(a, p-1, m) % m;
    }
}

// a^^n mod mを求める
ll H(ll a, ll n, ll m) {
    if (m == 1) return 0;
    if (n == 0) return 1;
    ll tmp = check(a, n);
    if (tmp < 3000) return tmp%m;
    return modpow(a, 3000+(H(a, n-1, phi[m])-3000)%phi[m], m);
}

int main() {
    initPhi();
    ll A, N, M;
    cin >> A >> N >> M;
    if (M == 1) {
        cout << 0 << endl;
    } else if (N == 0 || A == 1) {
        cout << 1 << endl;
    } else {
        cout << H(A, N, M) << endl;
    }
    return 0;
}
0