p4139 上帝与集合的正确用法

考察欧拉降幂,这道题因为是2的无限次方,所以要递归降幂。

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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#define N 6001
using namespace std;
long long p;
typedef long long ll ;
ll (ll x)
{
ll res=x,a=x;
for(ll i=2;i*i<=x;i++)
{
if(a%i==0)
{
res=res/i*(i-1);
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
ll qpow(ll a,ll b,ll p)
{
ll res = 1;
while(b)
{
if(b&1)
res = (res*a) % p;
a = (a*a) % p;
b>>=1;
}
return res;
}
ll f(ll p)
{
if(p==1)
return 0;
ll x = ph(p);
return qpow(2ll,f(x)+x, p);
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> p;
cout << f(p) << endl;
}
}