首页 > 代码库 > 快速幂

快速幂

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define mod 1003

int quick_power(int a, int b)
{
if(a == 0) return 0;
int ret = 1;
while(b)
{
if(b & 1)
ret = ret * a % mod;
a = a * a % mod;
b /= 2;
}
return ret;
}

int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int m, n;
scanf("%d %d", &m, &n);
printf("%d\n", quick_power(m % mod, n));
}
}

快速幂