• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
XD-TEST
博客园    首页    新随笔    联系   管理    订阅  订阅

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\n",calc(p));
53     }
54 }

 

posted @ 2016-07-20 13:45  fzmh  阅读(559)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3