Topic:
Given a A, b
Find out: A^a^a....^a (b a)
Input:
A, b
Output
Result of Operation
Examples:
2 3
Output: 16
Range: a,b<=10^9
We can get the answer first: ans=a^ (a^ (b-1))
However (a^ (b-1)) as the exponent is too big, must take the model
Make y= (a^ (b-1)), p=1e9+7
Y=k*φ (P) + (y%φ (p))
Because x^φ (p)ξ1 (mod p)
So a^φ (p)%p=1
So a^y=a^ (y%φ (p))
Because p is a prime number, φ (p) =p-1
So the a^ (b-1) mode p-1
You can do it with a quick power.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5 using namespacestd;6 intMod=1000000007;7 Long Longa,b,ans,s;8 Long LongQpow (Long LongXintYintmo)9 {Ten Long Longres=1; One while(y) A { - if(y&1) res= (res*x)%mo; -x= (x*x)%mo; they>>=1; - } - returnRes; - } + intMain () - { +Cin>>a>>b; AS=qpow (a,b-1, mod-1); atans=Qpow (a,s,mod); -cout<<ans; -}
Exponential modulus algorithm