結果

問題 No.181 A↑↑N mod M
ユーザー sugim48
提出日時 2015-04-06 01:13:31
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,341 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 85,524 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-24 03:09:21
合計ジャッジ時間 2,054 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v, w; };

ll MOD = 1000000009;
ll _MOD = 1000000009;
double EPS = 1e-10;

int euler[2001];

int f(int A, int N, int M) {
    if (M == 1) return 1;
    if (N == 0) return 1;
    int x = f(A, N - 1, euler[M]);
	if (A >= M * 2)
		A = M + A % M;
    int res = 1;
    for (; x; x /= 2) {
        if (x % 2) {
			res = res * A;
			if (res >= M * 2)
				res = M + res % M;
		}
        A = A * A;
		if (A >= M * 2)
			A = M + A % M;
    }
    return res;
}

int main() {
    for (int i = 0; i <= 2000; i++)
        euler[i] = i;
    for (int i = 2; i <= 2000; i++)
        if (euler[i] == i)
            for (int j = i; j <= 2000; j += i)
                euler[j] = euler[j] / i * (i - 1);
    int A, N, M; cin >> A >> N >> M;
    cout << f(A, N, M) % M << endl;
}
0