結果

問題 No.181 A↑↑N mod M
ユーザー tottoripaper
提出日時 2015-04-06 00:43:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,090 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 23,552 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-04 02:56:47
合計ジャッジ時間 1,141 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 25 WA * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:57:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   57 |     scanf("%lld %lld %lld", &A, &N, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

// Wrongri-La Shower

#include <cstdio>

typedef long long ll;

int phi[2001];
ll A, N, M;

int expt(int a, int n){
    int res = 1;
    while(n > 0){
        if(n & 1){res *= a;}
        a *= a;
        n >>= 1;
    }
    return res;
}

ll mod_pow(ll a, ll n, ll m){
    ll res = 1;
    while(n > 0){
        if(n & 1){res = res * a % m;}
        a = a * a % m;
        n >>= 1;
    }
    return res;
}

ll tower(ll a, ll n, ll m){
    // printf("%lld, %lld, %lld\n", A, N, M);
    if(n == 0ll){return 1ll;}
    if(m == 1ll){return 0ll;}

    return mod_pow(a, tower(a, n-1, phi[m]), m);
}

int main(){
    phi[1] = 1;
    for(int i=2;i<=2000;i++){
        phi[i] = 1;
        
        int x = i;
        for(int j=2;j*j<=x;j++){
            int count = 0;
            while(x % j == 0){
                x /= j;
                count += 1;
            }

            if(count == 0){continue;}
            phi[i] *= expt(j, count) - expt(j, count-1);
        }
        if(x > 1){phi[i] *= x - 1;}
    }
    
    scanf("%lld %lld %lld", &A, &N, &M);

    printf("%lld\n", tower(A, N, M));
}
0