結果
問題 | No.2193 メガの下1桁 |
ユーザー |
![]() |
提出日時 | 2023-01-13 22:44:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,178 bytes |
コンパイル時間 | 2,156 ms |
コンパイル使用メモリ | 175,928 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-24 18:37:37 |
合計ジャッジ時間 | 3,435 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 39 WA * 1 |
ソースコード
#include <bits/stdc++.h>using namespace std;//https://trap.jp/post/1444/constexpr uint32_t totient(uint32_t x){uint32_t ans = x;for(uint32_t i = 2; i * i <= x; i++) if(x % i == 0){ans /= i;ans *= i - 1;do x /= i; while(x % i == 0);}if(x != 1){ans /= x;ans *= x - 1;}return ans;}template<uint32_t P> struct Modint{static_assert(P < 0x80000000, "P must be smaller than 2^31");uint32_t a;Modint<totient(P)> b;private:static uint32_t mod(uint64_t x){if(x < P * 2) return uint32_t(x);return uint32_t(x % P) + P;}static uint32_t mul(uint32_t a, uint32_t b){return mod(uint64_t(a) * b);}static uint32_t pow(uint32_t a, uint32_t b){uint32_t ans = 1;while(b){if(b & 1) ans = mul(ans, a);a = mul(a, a);b >>= 1;}return ans;}public:Modint(uint64_t x): a(mod(x)), b(x){}Modint(uint32_t a, Modint<totient(P)> b): a(a), b(b){}uint32_t val() const {if(a < P) return a;return a - P;}Modint& operator*=(const Modint& other){a = mul(a, other.a);b *= other.b;return *this;}Modint operator*(const Modint& other) const {return Modint(*this) *= other;}Modint& operator+=(const Modint& other){a += other.a;if(a >= P * 2) a -= P;if(a >= P * 2) a -= P;b += other.b;return *this;}Modint operator+(const Modint& other) const {return Modint(*this) += other;}Modint pow(const Modint& other) const {return {pow(a, other.b.a), b.pow(other.b)};}bool operator==(const Modint& other) const {return a == other.a && b == other.b;}};template<> struct Modint<1>{uint32_t a;Modint(uint64_t x): a(bool(x)){}uint32_t val() const {return 0;}Modint& operator*=(const Modint& other){a &= other.a;return *this;}Modint operator*(const Modint& other) const {return Modint(*this) *= other;}Modint& operator+=(const Modint& other){a |= other.a;return *this;}Modint operator+(const Modint& other) const {return Modint(*this) += other;}Modint pow(const Modint& other) const {return {a || !other.a};}bool operator==(const Modint& other) const {return a == other.a;}};int main(){long long M;cin >> M;long long D;cin >> D;long long N;cin >> N;int B;cin >> B;Modint<3960> m = M, d = D;vector<Modint<3960>> C;C.push_back(m);int t1, t2;while (true){m = (m + d).pow(m);int cnt = C.size();bool ok = false;for (int i = 0; i < cnt; i++){if (C[i] == m){t1 = i;t2 = C.size();ok = true;}}C.push_back(m);if (ok){break;}}int ans;if (N <= t2){ans = C[N].a % B;} else {ans = C[(N - t1) % (t2 - t1) + t1].a % B;}if (ans < 10){cout << ans << endl;} else {cout << 'A' << endl;}}