問題のリンク - http://www.spoj.com/problems/LASTDIG/
要約 - 2 つの負でない整数 a と b が与えられた場合、a^b の最後の桁を出力します。
アルゴリズムを使用して、より少ないメモリ スペースを使用して剰余累乗を見つけようとしましたが ( http://en.wikipedia.org/wiki/Modular_exponentiation#Memory-effective_method )、ソリューションで TLE(Time Limit Exceeding) エラーが発生します。コードを 1 秒以内に実行するには、どのような変更を加える必要がありますか? 1 秒以内に 10 個のテスト ケースを実行する必要があることに注意してください。
私の解決策:
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
typedef long long LL;
using namespace std;
int ME(LL A, LL B)
{
LL c=1;
int E=0;
while(E<B)
{
c=(c*A)%10;
E++;
}
return c;
}
int main()
{
int t;
LL a, b;
vector <int> lDigit(31);
cin>>t;
for(int i=0;i<t;i++)
{
cin>>a>>b;
if(b>=99999)
lDigit[i]=ME(a, b);
else
{
int temp=pow(a, b);
lDigit[i]=temp%10;
}
}
for(int i=0;i<t;i++)
cout<<lDigit[i]<<endl;
return 0;
}