結果

問題 No.181 A↑↑N mod M
ユーザー glretoglreto
提出日時 2021-03-20 17:03:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,420 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 95,760 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 04:31:58
合計ジャッジ時間 2,210 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<complex>
#include<functional>
#include<stack>
#include<queue>
#include<set>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long ll;
typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
const int inf = 0x3fffffff;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod = 1e9 + 7;
unsigned int phi(unsigned int n) {
    unsigned int res = n;
    if (n % 2 == 0) { res = res / 2; do { n /= 2; } while (n % 2 == 0); }
    if (n % 3 == 0) { res = res / 3 * 2; do { n /= 3; } while (n % 3 == 0); }
    for (unsigned int i = 5; i * i <= n; i += 4) {
        if (n % i == 0) {
            res = res / i * (i - 1);
            do { n /= i; } while (n % i == 0);
        }i += 2;
        if (n % i == 0) {
            res = res / i * (i - 1);
            do { n /= i; } while (n % i == 0);
        }
    }if (n != 1) res = res / n * (n - 1);
    return res;
}unsigned int mod_pow(unsigned long long a, unsigned int b, unsigned int mod, bool flag) {
    unsigned long long res = 1;
    while (b) {
        if (b & 1) {
            if ((res *= a) >= mod) res %= mod, flag = true;
        }if ((a *= a) >= mod) a %= mod, flag = true;
        b >>= 1;
    }return res + (flag ? mod : 0);
}unsigned int rec(unsigned int a, unsigned int b, unsigned int mod) {
    if (a == 0) return !(b & 1);
    if (a == 1 || mod == 1) return 1;
    if (b == 1) return (a >= mod) ? (a % mod + mod) : a;
    if (b == 2) return mod_pow(a % mod, a, mod, (a >= mod));
    const unsigned int phi_val = phi(mod);
    const unsigned int res = rec(a, b - 1, phi_val);
    return mod_pow(a % mod, res, mod, (res >= phi_val));
}unsigned int tetration(unsigned int a, unsigned int b, unsigned int mod) {
    if (b == 0) return (mod != 1);
    const unsigned int res = rec(a, b, mod);
    return (res >= mod) ? (res - mod) : res;
}int main() {
    int a, n, m; cin >> a >> n >> m;
    if (n == 0)cout << 1 << endl;
    else cout << tetration(a, n, m) << endl;
}
0