【Bzoj3884】上帝与集合的正确用法

时间:2022-02-28 23:17:49

题目大意

2222222...mod p

分析

po
+2xaxa mod φ(p)+φ(p) (mod p)
f(n)=2222222...(mod n)
=2(222222... mod φ(n))+φ(n) (mod n)
=2f(φ(n))+φ(n) (mod n)
n==10(mod 10)
TLE

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

int t;
LL x;

LL phi(LL n){
LL tmp = n;
for(LL i=2;i*i<=n;i++)
if(n%i == 0){
tmp = tmp/i*(i-1);
while(n%i == 0) n /= i;
}
if(n > 1) tmp = tmp/n*(n-1);
return tmp;
}

LL pwr(LL n,LL k,LL p){
LL ans = 1;
while(k) {if(k&1) ans=(ans*n)%p;n=(n*n)%p;k>>=1;}
return ans;
}

LL f(LL p)
{
if(p==1) return 0;
LL phi_p=phi(p);
return pwr(2,f(phi_p)+phi_p,p);
}

int main(){
scanf("%d",&t);
while( t-- ){
scanf("%lld",&x);
printf("%lld\n",f(x));
}
return 0;
}