bzoj3884 上帝与集合的正确用法

  a^b mod P=a^(b mod phi(p)) mod p,利用欧拉公式递归做下去。

  代码

 1 #pragma comment(linker,"/STACK:1024000000,1024000000") 
 2 #include<cstdio>
 3 #include<string>
 4 #include<iostream>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<algorithm>
 9 #include<cstring>
10 #define fi first
11 #define sc second
12 #define pb push_back
13 using namespace std;
14 int p;
15 int f[10101010];
16 int ksm(int x,int p)
17 {
18     if (x==0) return 1;
19     long long ans=ksm(x/2,p);
20     ans=ans*ans%p;
21     if (x%2) ans=ans*2%p;
22     return ans;
23 }
24 int gao(int x)
25 {
26     int i,tmp=x,ans=x;
27     for (i=2;i*i<=tmp;i++)
28     if (tmp%i==0)
29     {
30         while (tmp%i==0) tmp/=i;
31         ans=ans/i*(i-1);
32     }
33     if (tmp>1)
34     ans=ans/tmp*(tmp-1);
35     return ans;
36 }
37 int calc(int x)
38 {
39     if (x==1) return 0;
40     int phi;
41     if (f[x]==0) f[x]=gao(x);
42     phi=f[x];
43     return (ksm(calc(phi)+phi,x));
44 }
45 int main()
46 {
47     int test;
48     scanf("%d",&test);
49     while (test--)
50     {
51     scanf("%d",&p);
52     printf("%d
",calc(p));
53     }
54 }
原文地址:https://www.cnblogs.com/fzmh/p/5688062.html