您的位置:首页 > 其它

BZOJ3884 LuoguP4139 上帝与集合的正确用法

2018-02-02 23:28 197 查看
扩展欧拉定理:a的b次方同余于a的b%phi(p)次方 (mod p) (gcd(a,p)=1)

http://blog.csdn.net/ez_yww/article/details/76176970

题解地址

http://blog.csdn.net/popoqqq/article/details/43951401

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
typedef long long ll;
using namespace std;
int T,p;
inline int phi(int x)//E(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…(1-1/pn),其中p1,p2……pn为x的所有素因数,x是不为0的整数。E(1)=1(唯一和1互质的数就是1本身)
{
int ans=x,now=x;
for(int i=2;i*i<=now;i++)
{
if(now%i==0)
{
ans=ans/i*(i-1);
while(now%i==0)now/=i;
}
}
if(now>1)//最后只剩下 小于4的素数  或者n本身就是素数
{
ans=ans/now*(now-1);
}
return ans;
}
inline int ksm(ll a,int b,int M)
{
ll ans=1;
while(b)
{
if(b&1)ans=(ans*a)%M;
a=(a*a)%M;
b>>=1;
}
return ans;
}
inline int solve(int p)
{
if(p==1)return 0;
int tmp=0;
while(!(p&1)){
tmp++;p>>=1;
}
int phi_p=phi(p);
int ans=solve(phi_p);
ans=(ans-tmp%phi_p+phi_p)%phi_p;
ans=ksm(2,ans,p)%p;
return ans<<tmp;
}
int main()
{
cin>>T;
while(T--)
{
cin>>p;
cout<<solve(p)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息