結果

問題 No.181 A↑↑N mod M
ユーザー yosupotyosupot
提出日時 2015-04-06 00:57:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,266 bytes
コンパイル時間 1,267 ms
コンパイル使用メモリ 160,528 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 18:54:07
合計ジャッジ時間 3,092 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 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 2 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,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 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,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll, ll> P;

// x^n % md
ll pow_mod(ll x, ll n, P md) {
    ll y = 1;
    for (int i = 0; i < n; i++) {
        y *= x;
        if (y > md.first) {
            y -= md.first;
            y %= md.second;
            y += md.first;
        }
    }
    return y;
/*    ll r = 1;
    while (n) {
        if (n & 1) {
            r = r * x;
            if (r > md.first) {
                r -= md.first;
                r %= md.second;
                r += md.first;
            }
        }
        x = x * x;
        if (x > md.first) {
            x -= md.first;
            x %= md.second;
            x += md.first;
        }
        n >>= 1;
    }
    return r;*/
}


// x^n % md
ll pow_mod(ll x, ll n, ll md) {
    ll r = 1;
    while (n) {
        if (n & 1) r = (r * x) % md;
        x = (x * x) % md;
        n >>= 1;
    }
    return r % md;
}

ll md(ll x, P m) {
    if (x > m.first) {
        x -= m.first;
        x %= m.second;
        x += m.first;
    }
    return x;
}
ll a;
P cycle(P m) {
    ll x = md(1, m);
    map<ll, ll> mp;
    mp[x] = 0;
    for (int i = 1; i < 100000; i++) {
        x *= a;
        x = md(x, m);
        if (mp.count(x)) {
            return {mp[x], i-mp[x]};
        }
        mp[x] = i;
    }
    assert(false);
}

ll solve(ll n, P m) {
    if (n == 0) {
        return md(1, m);
    }
    P mm = cycle(m);
    return pow_mod(a, solve(n-1, mm), m);
}

P cycleN(P m) {
    map<P, ll> mp;
    mp[m] = 0;
    for (int i = 1; i < 1e9; i++) {
        m = cycle(m);
        if (mp.count(m)) {
            return {mp[m], i-mp[m]};
        }
        mp[m] = i;
    }
    assert(false);
}
int main() {
    ll n, M;
    cin >> a >> n >> M;
    assert(1 <= a && a <= 1e9);
    assert(0 <= n && n <= 1e9);
    if (a == 1 || n == 0) {
        cout << 1 % M << endl;
        return 0;
    }
    if (M == 1) {
        cout << 0 << endl;
        return 0;
    }



    P p = cycle(P(0, M));
    if (n < 10000) {
        cout << pow_mod(a, solve(n-1, p), P(0, M)) << endl;
        return 0;
    }
    n = md(n, cycleN(P(0, M)));
    cout << pow_mod(a, solve(n-1, p), P(0, M)) << endl;
    return 0;
}
0