P4139 上帝与集合的正确用法

题目

P4139 上帝与集合的正确用法

分析

根据扩展欧拉定理,直接把柿子丢去递归,并且可以在 \(\log V\) 次内必定可以递归成 \(1\)

于是线性筛预处理一下 \(\varphi\) 即可。

代码

#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
    x=0;char ch=getchar();bool f=false;
    while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
}

template <typename T>
inline void write(T x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10^48);
    return ;
}
#define ll long long
#define ull unsigned long long
const int N=1e5+5;
unordered_map<ll,ll>phi;
ll n,m,q,a[N];
inline void chk(ll &x,ll mod){if(x>=mod) x%=mod,x+=mod;}
ll QuickPow(ll x,ll y,ll mod){ll res=1;while(y){if(y&1)	res=res*x,chk(res,mod);y>>=1;x=x*x;chk(x,mod);}return res;}
ll GetPhi(ll x){if(phi[x]!=0) return phi[x];ll ans=x;for(ll i=2;i*i<=x;i++){if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}}if(x>1)ans-=ans/x;return ans;}
ll dfs(ll now,ll mod){
	if(mod==1) return 1;
	ll Mi=dfs(now+1,phi[mod]);
	return QuickPow(2,Mi,mod);
}
signed main(){
	int t;read(t);
	while(t--){
		read(n);
		ll tmp=n;
		while(tmp>1) phi[tmp]=GetPhi(tmp),tmp=phi[tmp];
		phi[1]=1;
		write(dfs(1,n)%n),putchar('\n');
	}
	return 0;
}
posted @ 2021-08-23 10:05  __Anchor  阅读(44)  评论(0编辑  收藏  举报