結果

問題 No.181 A↑↑N mod M
ユーザー parukiparuki
提出日時 2016-09-02 14:27:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,912 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 166,948 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 19:03:03
合計ジャッジ時間 2,979 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 1 ms
4,380 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 1 ms
4,384 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,384 KB
testcase_37 AC 1 ms
4,384 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,384 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;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int M_MAX = 2010;

// a^k(k>=0) mod mの周期を求める。
int cycle(int a, int m){
    vector<int> vis(m, -1);
    ll cur = 1;
    int res = -1;
    for(int i = 0;res==-1; ++i){
        if(vis[cur] != -1)res = i - vis[cur];
        else vis[cur] = i;
        cur = cur*a%m;
    }
    return res;
}

ll powMod(ll n, ll k, ll mod){
    if(n==0)return 0;
    ll res = 1;
    while(k){
        if(k&1)res=res*n%mod;
        n=n*n%mod;
        k>>=1;
    }
    return res;
}

const int MOD_MAX = 2016;
vi preCalc;
ll tetrationMod(int a, ll b, int m){
    if(m == 1)return 0;
    if(b == 0)return 1;

    // a↑↑(b-1)がMOD_MAX未満
    if(b - 1 < sz(preCalc)){
        return powMod(a, preCalc[b - 1], m);
    }

    int c = cycle(a, m);
    ll after = tetrationMod(a, b - 1, c) - MOD_MAX;
    after %= c;
    if(after < 0)after += c;
    return powMod(a, MOD_MAX + after, m);
}

int main(){
    int A, N, M;
    cin >> A >> N >> M;
    if(A == 1){
        cout << 1 % M << endl;
        return 0;
    }

    preCalc.push_back(1);
    for(int i = 0; ; ++i){
        ll x = 1;
        int ok = 1;
        rep(j, preCalc.back()){
            x *= A;
            if(x >= MOD_MAX){
                ok = 0;
                break;
            }
        }

        if(!ok)break;
        assert(x > preCalc.back());
        preCalc.push_back((int)x);
    }
    cout << tetrationMod(A, N, M) << endl;
}
0