結果

問題 No.181 A↑↑N mod M
ユーザー yosupotyosupot
提出日時 2015-04-06 00:29:08
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,301 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 153,308 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-17 04:57:31
合計ジャッジ時間 5,374 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
}


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

int main() {
    ll a, n, m;
    cin >> a >> n >> m;
    assert(1 <= a && a <= 1e9);
    assert(0 <= n && n <= 1e9);
    assert(1 <= m && m <= 2000);
    if (a == 1 || n == 0) {
        cout << 1 % m << endl;
        return 0;
    }
    if (m == 1) {
        cout << 0 << endl;
        return 0;
    }
    n--;
    P p = cycle(a, m);
    assert(p.second);
    ll x = 1;
    if (n < 10000) {
        for (int i = 1; i <= n; i++) {
            x = pow_mod(a, x, p);
        }
        cout << pow_mod(a, x, m) << endl;
        return 0;
    }
    assert(false);
    map<ll, ll> mp;
    mp[x] = 0;
    bool f = false;
    for (int i = 1; i < 100000; i++) {
        x = pow_mod(a, x, p);
        if (mp.count(x)) {
            f = true;
            if (n < mp[x]) break;
            n -= mp[x];
            n %= i - mp[x];
            n += mp[x];
            break;
        }
        mp[x] = i;
    }
    assert(f);
    x = 1;
    for (int i = 0; i < n; i++) {
        x = pow_mod(a, x, p);
    }
    cout << pow_mod(a, x, m) << endl;
    return 0;
}
0