結果
問題 | No.181 A↑↑N mod M |
ユーザー | Pachicobue |
提出日時 | 2017-11-18 16:07:54 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 3,973 bytes |
コンパイル時間 | 2,286 ms |
コンパイル使用メモリ | 208,800 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-05-04 04:38:08 |
合計ジャッジ時間 | 3,197 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,944 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,940 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,944 KB |
testcase_19 | AC | 1 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,944 KB |
testcase_21 | AC | 2 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,940 KB |
testcase_23 | AC | 2 ms
6,940 KB |
testcase_24 | AC | 2 ms
6,940 KB |
testcase_25 | AC | 2 ms
6,940 KB |
testcase_26 | AC | 2 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | AC | 1 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 2 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,944 KB |
testcase_32 | AC | 2 ms
6,944 KB |
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,940 KB |
testcase_39 | AC | 2 ms
6,944 KB |
testcase_40 | AC | 1 ms
6,940 KB |
testcase_41 | AC | 1 ms
6,940 KB |
testcase_42 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template <typename T> constexpr T INF = numeric_limits<T>::max() / 100; ll A, N, M; vector<ll> prime; template <typename T> T extgcd(const T a, const T b, T& x, T& y) // ax+by=gcd(a,b) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll power(const ll n, const ll mod) { if (n == 0) { return 1; } if (n % 2 == 1) { return (mod != -1) ? ((power(n - 1, mod) * A) % mod) : (power(n - 1, mod) * A); } else { const ll pp = power(n / 2, mod); return (mod != -1) ? ((pp * pp) % mod) : (pp * pp); } } template <typename T> pair<T, T> ChineseRemainderTheorem(const pair<T, T> a1, const pair<T, T> a2) { assert(gcd(a1.first, a2.first) == 1); const T p1 = a1.first; const T m1 = a1.second; const T p2 = a2.first; const T m2 = a2.second; if (m1 == m2) { return make_pair(p1 * p2, m1); } else { T x, y; extgcd(p1, p2, x, y); assert(p1 * x + p2 * y == 1); x *= (m2 - m1); y *= (m2 - m1); const T p = p1 * p2; return make_pair(p, (((p1 * x + m1) % p) + p) % p); } } ll over(const ll n, const long double x) // A↑↑n >= x なら 0, それ以外なら A↑↑n { if (n == 0) { return (x <= 1) ? 0 : 1; } if (x <= 1) { return 0; } const long double lx = log2(x) / log2(A); const ll p = over(n - 1, lx); if (p == 0) { return 0; } else { return power(p, -1); } } ll rem(const ll n, const ll mod) // A↑↑n (mod m) { if (n == 0) { return 1 % mod; } else if (mod == 1) { return 0; } else { ll num = mod; pair<ll, ll> result = make_pair(1, 0); for (const ll p : prime) { if (num % p == 0) { ll beki = 1; ll cnt = 0; while (num % p == 0) { beki *= p; num /= p; cnt++; } const ll phi = beki / p * (p - 1); pair<ll, ll> fac = make_pair(beki, 1); ll a = A; int count = 0; while (a % p == 0) { count++; a /= p; } if (count > 0) { const ll ind = over(n - 1, (long double)cnt / count); if (ind == 0) { fac.second = 0; } else { fac.second = power(ind, fac.first); } } else { const ll m = rem(n - 1, phi); fac.second = power(m, fac.first); } result = ChineseRemainderTheorem(result, fac); } if (num == 1) { break; } } return result.second; } } int main() { cin >> A >> N >> M; vector<bool> isprime(M + 1, true); for (ll i = 2; i <= M; i++) { if (isprime[i]) { for (ll j = 2; i * j <= M; j++) { isprime[i * j] = false; } } } for (ll i = 2; i <= M; i++) { if (isprime[i]) { prime.push_back(i); } } cout << rem(N, M) << endl; return 0; }