1. 程式人生 > 資訊 >德國有關部門致信庫克,希望蘋果重新考慮 CSAM 計劃

德國有關部門致信庫克,希望蘋果重新考慮 CSAM 計劃

題目

CF906D Power Tower

分析

首先根據擴充套件尤拉定理,我們可以得到一個遞迴柿子。

又考慮到最多遞迴 \(\log\) 次,於是可以直接列舉遞迴即可。

注意快速冪的取模要滿足擴充套件尤拉定理,同時 \(\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 n){ll ans=n;for(ll i=2;i*i<=n;i++){if(n%i==0){ans-=ans/i;while(n%i==0)n/=i;}}if(n>1)ans-=ans/n;return ans;}
ll dfs(ll now,ll r,ll mod){
	if(now==r+1||mod==1) return 1;
	ll Mi=dfs(now+1,r,phi[mod]);
	return QuickPow(a[now],Mi,mod);
}
signed main(){
	read(n),read(m);
	ll tmp=m;
	while(tmp>1) phi[tmp]=GetPhi(tmp),tmp=phi[tmp];phi[1]=1;
	for(int i=1;i<=n;i++) read(a[i]);
	read(q);
	while(q--){
		ll l,r;
		read(l),read(r);
		write(dfs(l,r,m)%m),putchar('\n');
	}
	return 0;
}