赛事 T1 我用快速幂:
#include<bits/stdc++.h>
using namespace std;
const long long ttt=31622;
long long Pow(long long a,long long b)
{
long long ret=a,sum=1;
while(b!=1)
{
if(b%2==0)
{
if(ret>ttt) return -1;
ret*=ret;
b/=2;
}
else
{
sum*=ret;
b--;
}
}
if(sum*ret>1e9||sum*ret<-1) return -1;
return sum*ret;
}
int main()
{
long long a,b;
scanf("%lld%lld",&a,&b);
cout<<Pow(a,b);
return 0;
}
可是这样也能过洛谷自测耶:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if(pow(a,b)>1e9)
{
cout<<-1;
}
else
{
cout<<(int)pow(a,b);
}
return 0;
}