这篇文章上次修改于 231 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

题目传送门

在$a,b>0,0$ $\leq$ $i$ $\leq$ $b$的情况下,肯定满足$a^b$ $\geq$ $a^{b-i}$,所以在快速幂的时候发现大于$10^9$就返回$1$。

十年OI一场空,不开long long见祖宗。

十年OI一场空,没有freopen见祖宗。

#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
int pow(int a,int b){
    if(b==0){
        return 1;
    }
    if(b%2==1){
        int temp=pow(a,b-1);
        if(temp==-1){
            return -1;
        }
        if(temp*a>1000000000ll){
            return -1;
        }
        return temp*a;
    }else{
        int temp=pow(a,b/2);
        if(temp==-1){
            return -1;
        }
        if(temp*temp>1000000000ll){
            return -1;
        }
        return temp*temp;
    }
}
int mian(){
    int a,b;
    scanf("%lld%lld",&a,&b);
    printf("%lld",pow(a,b));
    return 0;
}

欢迎到博客看其他题解。