結果

問題 No.181 A↑↑N mod M
ユーザー t98slidert98slider
提出日時 2022-07-21 00:05:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,072 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 171,008 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-15 10:21:33
合計ジャッジ時間 3,122 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct stable_int{
    long long v;
    stable_int() : v(0) {}
    stable_int(long long _v) : v(_v){}
    stable_int& operator++() {
        if(__builtin_add_overflow(v, 1, &v))v = std::numeric_limits<long long>::max();
        return *this;
    }
    stable_int& operator--() {
        if(__builtin_sub_overflow(v, 1, &v))v = std::numeric_limits<long long>::min();
        return *this;
    }
    stable_int operator++(int) {
        stable_int result = *this;
        ++*this;
        return result;
    }
    stable_int operator--(int) {
        stable_int result = *this;
        --*this;
        return result;
    }
    stable_int& operator+=(const stable_int& rhs) {
        if(__builtin_add_overflow(v, rhs.v, &v))v = std::numeric_limits<long long>::max();
        return *this;
    }
    stable_int& operator-=(const stable_int& rhs) {
        if(__builtin_sub_overflow(v, rhs.v, &v))v = std::numeric_limits<long long>::min();
        return *this;
    }
    stable_int& operator*=(const stable_int& rhs) {
        long long pre = v;
        if(__builtin_mul_overflow(v, rhs.v, &v)){
            v = (pre > 0) ^ (rhs.v > 0) ? std::numeric_limits<long long>::min()
                                        : std::numeric_limits<long long>::max();
        }
        return *this;
    }
    stable_int& operator/=(const stable_int& rhs) {
        v /= rhs.v;
        return *this ; 
    }
    stable_int operator+() const { return *this; }
    stable_int operator-() const { return stable_int() - *this; }
    friend stable_int operator+(const stable_int& lhs, const stable_int& rhs) {
        return stable_int(lhs) += rhs;
    }
    friend stable_int operator-(const stable_int& lhs, const stable_int& rhs) {
        return stable_int(lhs) -= rhs;
    }
    friend stable_int operator*(const stable_int& lhs, const stable_int& rhs) {
        return stable_int(lhs) *= rhs;
    }
    friend stable_int operator/(const stable_int& lhs, const stable_int& rhs) {
        return stable_int(lhs) /= rhs;
    }
    friend bool operator==(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v == rhs.v);
    }
    friend bool operator!=(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v != rhs.v);
    }
    friend bool operator<(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v < rhs.v);
    }
    friend bool operator<=(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v <= rhs.v);
    }
    friend bool operator>(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v > rhs.v);
    }
    friend bool operator>=(const stable_int& lhs, const stable_int& rhs) {
        return (lhs.v >= rhs.v);
    }
    friend istream& operator>>(istream& is,stable_int& rhs) noexcept {
        long long _v;
        rhs = stable_int{(is >> _v, _v)};
        return is;
    }
    friend ostream& operator << (ostream &os, const stable_int& rhs) noexcept {
        return os << rhs.v;
    }
};

int main(){
    int A, N, M;
    cin >> A >> N >> M;
    function<int(int, int, int)> rec = [&](int V, int N, int MOD){
        if(N == 0)return 1;
        if(MOD == 1)return 1000000;
        vector<int> order, tb(MOD, -1);
        int val = 1, tim = 0;
        while(tb[val] == -1){
            tb[val] = tim++;
            order.push_back(val);
            val *= V % MOD;
            val %= MOD;
        }
        int EXP = rec(V, N - 1, (tim - tb[val]));
        int idx = (EXP - tb[val]) % (tim - tb[val]) + tb[val];
        return order[idx] + 1000 * MOD;
    };
    function<int(int, int)> rec2 = [&](int V, int N){
        if(N == 0)return 1;
        int EXP = rec2(V, N - 1);
        stable_int v = 1, c = V;
        while(EXP){
            if(EXP & 1)v *= c;
            c *= c;
            EXP >>= 1;
        }
        return int(min(v.v, 1ll << 30));
    };
    int ans = rec(A, N, M) % M;
    int ans2 = (N <= 20 ? rec2(A, N) : 1000000);
    if(ans2 < 1000000){
        cout << ans2 % M << endl;
    }else{
        cout << ans << endl;
    }
}
0