#20235: 快速冪一樣TLE


fdhs109_31813 (username)

學校 : 桃園市私立復旦高級中學
編號 : 103932
來源 : [140.112.25.14]
最後登入時間 :
2024-04-26 21:20:38
d475. 玩具求幂题(求幂系列题2) -- scientific | From: [220.133.254.202] | 發表日期 : 2019-12-14 19:39

#include<iostream>
#define _ ios::sync_with_stdio(false);cin.tie(0);
#define endl '\n'
using namespace std;
long long qpow(long long a,long long n)
{
if(a==1)
return 1;
else if(a==0)
return 0;
else if(n==0)
return 1;
else if(n==1)
return a;
else if(n%2==0)
return qpow(a,n/2)*qpow(a,n/2);
else
return qpow(a,n/2)*qpow(a,n/2)*a;
}
int main()
{_
long long a,n;
int count=0;
while(cin>>a>>n)
{
count++;
if(a==0&&n==0)
{
cout<<"All Over. Exceeded "<<count<<" lines!\n";
break;
}
cout<<qpow(a,n)<<endl;
}
}

求解哪裡慢了,1跟0的次方特判都有寫啊

 
#23429: Re:快速冪一樣TLE


kr98k01622354 (弱い)

學校 : 臺北市立建國高級中學
編號 : 103381
來源 : [1.171.232.97]
最後登入時間 :
2023-10-21 00:22:00
d475. 玩具求幂题(求幂系列题2) -- scientific | From: [220.136.147.1] | 發表日期 : 2020-11-16 18:13

#include
#define _ ios::sync_with_stdio(false);cin.tie(0);
#define endl '\n'
using namespace std;
long long qpow(long long a,long long n)
{
if(a==1)
return 1;
else if(a==0)
return 0;
else if(n==0)
return 1;
else if(n==1)
return a;
else if(n%2==0)
return qpow(a,n/2)*qpow(a,n/2);
else
return qpow(a,n/2)*qpow(a,n/2)*a;
}
int main()
{_
long long a,n;
int count=0;
while(cin>>a>>n)
{
count++;
if(a==0&&n==0)
{
cout<<"All Over. Exceeded "<<count<<" lines!\n";
break;
}
cout<<qpow(a,n)<<endl;
}
}

求解哪裡慢了,1跟0的次方特判都有寫啊


-1

 
ZeroJudge Forum