2019ICPC Nanjing network game B super_log

Meaning of the questions: seeking a of a of a power. . B been seeking views, i.e. a number written on paper, and has a power of a power of m modulo, referred to as F (a, b, m) = pow (a, F (a, b-1 , phi (m))

Problem-solving ideas: Contact Euler descending, this iterative process, we have been seeking Euler function of m, then the Euler function on this result, it is clear that the process will not be more than the number of iterations, verify the range of available 1e6 Up to 19 times iteration,

But the problem there is a pit, must take the fast power mod + mod, it does not appear the result is 0, 0 causes must be something wrong (wa)

AC Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
bitset<maxn>notprime;
int phi[maxn],prime[maxn],cnt=0;
void pre(){
    phi[1]=1;
    for(int i=2;i<=maxn-5;i++){
        if(!notprime[i]){
            prime[++cnt]=i;
            phi[i]=i-1; // i is a prime number, Phi [i] = i-. 1 
        }
         for ( int J = . 1 ; J <= CNT && Prime [J] * i <= MAXN; J ++ ) { 
            notprime [i * Prime [J]] = . 1 ;
             IF (I% Prime [J] == 0 ) { // each being only a few of its smallest prime factor to screen out 
                Phi [Prime * I [J]] Phi = [I] * Prime [J];
                 // when a and b are coprime, satisfy phi (a * b) = phi (a) * phi (b), multiplicative function 
                BREAK ; 
            } 
            the else Phi [Prime * I [J]] Phi = [I] * (Prime [J] - . 1 );
             // Phi [Prime * I [J]] Phi = [I] * Phi [Prime [J]] Phi = [I] * (Prime [J] -1);
        }
    }
}
ll quick_mod(ll a,ll n,ll mod)
{
    ll res=1;
    while (n)
    {
        if(n&1)res=res*a>mod?res*a%mod+mod:res*a;
        a=a*a>mod?a*a%mod+mod:a*a;
        n>>=1;
    }
    return res;
}
ll deal(ll a,ll b,ll m)
{
    if(b==0)return 1;
    if(m==1)return 1;
    ll res=deal(a,b-1,phi[m]);
    return quick_mod(a,res,m);
}
int main(){
    pre();
    int t;
    cin>>t;
    ll a,b,m;
    while (t--)
    {
        cin>>a>>b>>m;
        cout<<deal(a,b,m)%m<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/xusirui/p/11443214.html