标签:char -- using font tchar mod $2 nbsp ola
题意:求$2^{2^{2^{2^{...}}}} \mod p$的值。$p \leq 10^7$
最开始想到的是$x \equiv x^2 \mod p$,然后发现不会做。。。
我们可以想到拓展欧拉定理:$a^b \equiv a^{b \mod \varphi (p) + \varphi (p)} \mod p$,而当$b < p$时有更强的结论$a^b \equiv a^{b \mod \varphi (p)} \mod p$。我们发现利用拓展欧拉定理可以递归下去处理$2^{2^{2^{2^{...}}}} \mod \varphi (p)$的问题,直到$\varphi (p)$为$1$时得到答案$0$。
1 #include<bits/stdc++.h>
2 using namespace std;
3
4 inline int read(){
5 int a = 0;
6 bool f = 0;
7 char c = getchar();
8 while(!isdigit(c)){
9 if(c == ‘-‘)
10 f = 1;
11 c = getchar();
12 }
13 while(isdigit(c)){
14 a = (a << 3) + (a << 1) + (c ^ ‘0‘);
15 c = getchar();
16 }
17 return f ? -a : a;
18 }
19
20 int prime[2010] , cntPrime;
21 bool isprime[5010];
22
23 inline int ola(int x){
24 int sum = x;
25 for(int i = 1 ; i * i <= x && i <= cntPrime ; i++)
26 if(x % prime[i] == 0){
27 while(x % prime[i] == 0)
28 x /= prime[i];
29 sum = sum / prime[i] * (prime[i] - 1);
30 }
31 if(x - 1)
32 sum = sum / x * (x - 1);
33 return sum;
34 }
35
36 inline int poww(long long a , long long b , int mod){
37 int times = 1;
38 while(b){
39 if(b & 1)
40 times = times * a % mod;
41 a = a * a % mod;
42 b >>= 1;
43 }
44 return times;
45 }
46
47 long long dfs(int x){
48 if(x == 1)
49 return 0;
50 return poww(2 , ola(x) + dfs(ola(x)) , x);
51 }
52
53 int main(){
54 for(int i = 2 ; i <= 5000 ; i++)
55 if(!isprime[i]){
56 prime[++cntPrime] = i;
57 for(int j = i ; j * i <= 5000 ; j++)
58 isprime[j * i] = 1;
59 }
60 for(int T = read() ; T ; T--){
61 int a = read();
62 cout << dfs(a) << endl;
63 }
64 return 0;
65 }
标签:char -- using font tchar mod $2 nbsp ola
原文地址:https://www.cnblogs.com/Itst/p/9865495.html