标签:std lld def upload scan abc contest cpp test
题意:
- 一个序列有\(n\)个数,\(q\)次询问,将\(l\)到\(r\)这个区间的数叠起来模上\(M\)
思路:
- 欧拉降幂
#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<ll,ll>mp;
const int M=1e5+10;
ll a[M];
ll ol(ll x)
{
if(mp[x])
{
return mp[x];
}
ll ans=x;
ll n=x;
for(int i=2;i*i<=n;i++)
{
if(x%i==0)
{
ans=ans*(i-1)/i;
}
while(x%i==0)
{
x/=i;
}
}
if(x>1)
{
ans=ans*(x-1)/x;
}
mp[n]=ans;
return ans;
}
ll query(ll a,ll mod)
{
if(a<mod)
{
return a;
}
else
{
return a%mod+mod;
}
}
ll pow(ll a,ll n,ll mod)
{
ll ans=1;
ll base=a;
while(n)
{
if(n&1)
{
ans=query(ans*base,mod);
}
base=query(base*base,mod);
n>>=1;
}
return ans;
}
ll solve(int l,int r,ll mod)
{
if(r==l||mod==1)
{
return query(a[l],mod);
}
ll d=solve(l+1,r,ol(mod));
return pow(a[l],d,mod);
}
int main( )
{
int n,q,l,r;
ll mod;
//cout<<ol(28)<<endl;
scanf("%d%lld",&n,&mod);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&l,&r);
printf("%lld\n",solve(l,r,mod)%mod);
}
return 0;
}
标签:std lld def upload scan abc contest cpp test
原文地址:https://www.cnblogs.com/lcbwwy/p/13125159.html