快速求幂取摸
a^b mod m! 0<a,b<1000000000. 思路:用for循环循环b次,求得答案后再取摸,肯定超时!
上代码:
#include<iostream>
#include<string>
#include <cstdio>
#include <cmath>
#include<vector>
#include<algorithm>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
__int64 a,b,m;
int n;
cin>>n;
while(n--)
{
cin>>a>>b>>m;
a%=m;
__int64 tmp=1;
while(b)
{
if(b%2==0)
{
a=(a*a)%m;
b/=2;
}
else {
tmp=tmp*a%m;
b-=1;
}
}
cout<<tmp<<endl;
}
return 0;
}
sample inpout:
3
2 1 10000
2 10 10000
3 10 10000
sample output:
2
1024
9049