結果
問題 | No.181 A↑↑N mod M |
ユーザー |
|
提出日時 | 2017-01-03 15:01:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,376 bytes |
コンパイル時間 | 886 ms |
コンパイル使用メモリ | 89,024 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-16 06:18:58 |
合計ジャッジ時間 | 2,302 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 6 |
other | AC * 37 |
ソースコード
#include <iostream>#include <vector>#include <map>#include <functional>#include <cmath>#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))using ll = long long;using namespace std;struct sequence {vector<int> data;int offset, cycle;};sequence iterate(int a, function<int (int)> f) {sequence xs;map<int, int> used;while (not used.count(a)) {used[a] = xs.data.size();xs.data.push_back(a);a = f(a);}xs.offset = used[a];xs.cycle = xs.data.size() - xs.offset;return xs;}int at(sequence const & xs, int i) {return xs.data[i < xs.offset ? i : (i - xs.offset) % xs.cycle + xs.offset];}double tet(int a, int n) {double acc = 1;repeat (i,n) {acc = pow(a, acc);if (isinf(acc)) break;}return acc;}int tetmod(int a, int n, int m) {if (m == 1) return 0;if (n == 0) return 1;if (a == 0) return 0;if (a == 1) return 1;double estimated = tet(a, n-1);sequence powmod = iterate(1, [&](int x) { return a *(ll) x % m; });ll b = tetmod(a, n-1, powmod.cycle);int i = estimated < powmod.offset ? estimated : ((b - powmod.offset) % powmod.cycle + powmod.cycle) % powmod.cycle + powmod.offset;return powmod.data[i];}int main() {int a, n, m; cin >> a >> n >> m;cout << tetmod(a, n, m) << endl;return 0;}