結果
問題 | No.181 A↑↑N mod M |
ユーザー | Pachicobue |
提出日時 | 2017-11-18 15:41:46 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,222 bytes |
コンパイル時間 | 2,047 ms |
コンパイル使用メモリ | 217,672 KB |
実行使用メモリ | 814,848 KB |
最終ジャッジ日時 | 2024-05-04 04:37:00 |
合計ジャッジ時間 | 7,073 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | WA | - |
testcase_08 | MLE | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
ソースコード
#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; map<ll, ll> A_factor; 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 (1 >= x) ? 0 : 1; } 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; vector<pair<ll, ll>> fac; vector<ll> phi; vector<ll> count; for (const ll p : prime) { if (num % p == 0) { ll beki = 1; ll cnt = 0; while (num % p == 0) { beki *= p; num /= p; cnt++; } fac.push_back(make_pair(beki, 1)); phi.push_back(beki / p * (p - 1)); count.push_back(cnt); } if (num == 1) { break; } } const ll size = fac.size(); for (ll i = 0; i < size; i++) { if (A_factor[fac[i].first] > 0) { const ll ind = over(n - 1, (long double)A_factor[fac[i].first] / count[i]); if (ind == 0) { fac[i].second = 0; } else { fac[i].second = power(ind, fac[i].first); } } else { const ll m = rem(n - 1, phi[i]); fac[i].second = power(m, fac[i].first); } } pair<ll, ll> result = fac[0]; for (ll i = 1; i < size; i++) { result = ChineseRemainderTheorem(result, fac[i]); } 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); } } ll num = A; for (const ll p : prime) { ll cnt = 0; while (num % p == 0) { cnt++; num /= p; } A_factor[p] = cnt; } cout << rem(N, M) << endl; return 0; }