p107p \le 10^7

LG P4139

Knowledge

扩展欧拉定理:

Solution

递归次数O(logp)O(\log p)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <climits>
#include <queue>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
#include <fstream>
#include <tr1/unordered_map>
#include <assert.h>

#define x first
#define y second
#define y0 Y0
#define y1 Y1
#define mp make_pair
#define pb push_back
#define DEBUG(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int MAXN = 1e7;

int P;
int phi[MAXN + 5], prime[MAXN / 10 + 5];
bitset <MAXN + 5> vis;

inline void Init ()
{
for (int i = 2; i <= MAXN; ++i)
{
if (!vis[i])
{
prime[++prime[0]] = i;
phi[i] = i - 1;
}
for (int j = 1; j <= prime[0] && (LL) i * prime[j] <= MAXN; ++j)
{
vis[i * prime[j]] = 1;
if (!(i % prime[j]))
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else phi[i * prime[j]] = phi[i] * phi[prime[j]];
}
}
}

inline int Pow (int a, int b, int p)
{
int ans = 1;
for (int i = b; i; i >>= 1, a = (LL) a * a % p) if (i & 1) ans = (LL) ans * a % p;
return ans;
}

inline int calc (int p)
{
if (p == 1) return 0;
if (p == 2) return 0;
return Pow (2, calc (phi[p]) + phi[p], p);
}

inline void Solve () { printf("%d\n", calc (P)); }

inline void Input () { P = read<int>(); }

int main ()
{

#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Init ();

int Te = read<int>();
while (Te--)
{
Input ();
Solve ();
}

return 0;
}