Description
DYY 很善于解梦,昨晚,他梦见自己来到了一个高度发达的国度。
众所周知,我们现在有极为常用的三级运算,+、、^。其中,a*b=a+a+a+…+a(b 个a),a^b=a*a*a…*a(b 个a)。但是,在这个国家,还有第四级运算——♂,a♂b=a^a^a^…^a(b 个a,从左往右计算)。同时,由于这个国家的历史背景,他们非常反感高精度,所以a♂b 的结果是经过1e9+7 取模的。
DYY 觉得这个梦可能意味着他将成为全球第一的解梦第二的数学之神,但要彻底解开它,DYY 必须算出来a♂b 的结果。
Input
第一行:两个正整数,a 和b。
Output
一个整数,a♂b mod 1e9+7。
Sample Input
2 4
Sample Output
256
Data Constraint
Hint
2♂4=2^2^2^2=4^2^2=16^2=256
Solution
题意:给定 a 和
b ,求 (...((aa)a)a...)a ( b 个a ) 。不难想到这个东西就是:
aab−1然后快速幂跑就可以了。
要注意一下, ap−1≡1(mod p) ,所以在计算 ab−1 时,模 109+6 。
时间复杂度 O(log a+log b) 。
Code
#include<cstdio>
using namespace std;
typedef long long LL;
const int mo=1e9+7;
LL n,m;
inline LL ksm(LL x,LL y,int z)
{
LL s=1;
while(y)
{
if(y&1) s=s*x%z;
x=x*x%z;
y>>=1;
}
return s;
}
int main()
{
scanf("%lld%lld",&n,&m);
printf("%lld",ksm(n,ksm(n,m-1,mo-1),mo));
return 0;
}