【题解】 P4139 上帝与集合的正确用法
生活随笔
收集整理的這篇文章主要介紹了
【题解】 P4139 上帝与集合的正确用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
\(Decription:\)
給出T組詢問,每次給出一個p,求\(2^{2^{2^{2...}}} \mod {p}\)
\(Sample\) \(Input:\)
3
2
3
6
\(Sample\) \(Output:\)
0
1
4
一看這種題就是要降冪大法,每次把指數變小,遞歸求指數。
傳入一個參數:當前的模數,每次用歐拉定理做一下。
復雜度:O(能過)
#include<bits/stdc++.h> #define int long long using namespace std; int T,tmp,cnt,x; const int N=1e7; int phi[N+2],p[N+2]; bool vis[N+5]; inline int power(int a,int b,int p){int ret=1;while(b>0){if(b&1) ret=(ret*a)%p;a=(a*a)%p;b>>=1;} return ret; } inline int solve(int x){if(x==1) return 0;if(!(x&1))return power(2,solve(phi[x])+phi[x],x)%x;else return power(2,solve(phi[x]),x)%x; } signed main(){vis[1]=1;phi[1]=1;for(int i=2;i<=N;++i){if(!vis[i]) p[++cnt]=i,phi[i]=i-1;for(int j=1;j<=cnt && i*p[j]<=N;++j){vis[i*p[j]]=1;if(i%p[j]!=0) phi[i*p[j]]=phi[i]*phi[p[j]];else { phi[i*p[j]]=phi[i]*p[j];break;}}}scanf("%lld",&T);while(T--){scanf("%lld",&x);printf("%lld\n",solve(x));}return 0; }轉載于:https://www.cnblogs.com/JCNL666/p/10675944.html
總結
以上是生活随笔為你收集整理的【题解】 P4139 上帝与集合的正确用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android ViewModel+li
- 下一篇: SSH工具Secure Shell Cl