Codeforces 906 Power Tower

Topic Source: https://codeforces.com/contest/906/problem/D

Meaning of the questions is very simple, find the power modulo value.

Incidentally, each time a judgment say a ^ b% p, and b is determined phi (p) in the magnitude relation,

Violence determine if it is taken to determine the size of the log, bigwigs always a way to simplify adding a

ll MOD(ll n,ll mod)
{
    return n<mod?n:(n%mod+mod);
}

Cleverly avoid judgment, which is the size in comparison b and phi (p) a.

In fact, that is,v ^ {w 0 (w 1 v ^ {(W 2 ...))}, phi (p)))} mod p

Proves to be seen: https://www.cnblogs.com/ACMLCZH/p/8117161.html

Remember calculated before the map memory storage with euler.

1e7 start thinking about the play table, larger than the calculation again, and she did not card the past ........

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
const ll MAX=1e7;
map<ll,ll>euler;
ll phi[MAX],a[100100];
ll MOD(ll n,ll mod)
{
    return n<mod?n:(n%mod+mod);
}
void Euler()
{
     phi[1]=1;  
     for(ll i=2;i<MAX;i++)  
       phi[i]=i;  
     for(ll i=2;i<MAX;i++)  
        if(phi[i]==i)
           for(ll j=i;j<MAX;j+=i)  
              phi[j]=phi[j]/i*(i-1);
}
 
ll quickpow(ll a,ll n,ll mod)
{
    ll res=1;
    a=MOD(a,mod);
    while(n)
    {
        if(n&1)
        {
             res=MOD(a*res,mod);
        }
        n>>=1;
         a=MOD(a*a,mod);
    }
    return res;
}
ll euler1(ll n)
{
    ll now=n;
    ll ret=n;
    if(euler.count(now)) return euler[now];
    for(ll i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            ret=ret/i*(i-1);
            while(n%i==0)
                n/=i;
        }
    }
    if(n>1)
        ret=ret/n*(n-1);
    euler[now]=ret;
    return ret;
}
ll solve(ll l,ll r,ll mod)
{
    if(l==r||mod==1)
    return MOD(a[l],mod);
    {
    	return quickpow(a[l],solve(l+1,r,euler1(mod)),mod);
	}
}
int main()
{
//	Euler();
	ll  n,i,j,mod;
	scanf("%I64d %I64d",&n,&mod);
	for(i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
	}
	int q;
	scanf("%d",&q);
	while(q--)
	{
		ll l,r,d,b;
		scanf("%I64d %I64d",&l,&r);
		{
			ll as=solve(l,r,mod)%mod;
			printf("%I64d\n",as);
		}
	}
	return 0;
}

 

Published 56 original articles · won praise 17 · views 2322

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/102229138