题目描述
传送门
题目大意: 2222… mod p
题解
令
p=2k∗q
2222…modp
因为q与2互质,所以根据欧拉定理
aϕ(p)≡1( mod p)
原式=2k(2(222…−k) mod ϕ(q)mod q)
然后递归求解
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
int getphi(int x)
{
int ans=x;
for (int i=2;i*i<=x;i++)
if (x%i==0) {
ans=ans/i*(i-1);
while (x%i==0) x/=i;
}
if (x!=1) ans=ans/x*(x-1);
return ans;
}
LL quickpow(LL num,int x,LL p)
{
LL ans=1; LL base=num%p;
while (x) {
if (x&1) ans=ans*base%p;
x>>=1;
base=base*base%p;
}
return ans;
}
int solve(int p)
{
if (p==1) return 0;
int t=1; int cnt=0;
while (p%2==0) t*=2,p/=2,cnt++;
int phi=getphi(p);
int re=solve(phi);
re=((re-cnt)%phi+phi)%phi;
return (LL)t*quickpow(2,re,p);
}
int main()
{
freopen("a.in","r",stdin);
int T; scanf("%d",&T);
while (T--){
int x; scanf("%d",&x);
printf("%d\n",solve(x)%x);
}
}