Leetcode刷題92. 反轉連結串列 II
阿新 • • 發佈:2021-01-12
應用
求a^b的後三位數
#include <iostream>
#include <cstdio>
using namespace std;
int quick_pow(long long a, long long b, int p);
int main()
{
long long a, b;
int p = 1000;
while (true)
{
cin >> a >> b;
cout << quick_pow(a, b, p) << endl;
}
return 0 ;
}
int quick_pow(long long a, long long b, int p)
{
int ans = 1;
a = a % p;
while (b)
{
if (b & 1)// if(b % 2 == 1 )
{
ans = (ans%p) * (a%p) % p;// (a * b) % p = (a % p) * (b % p) % p
}
a = (a%p) * (a%p);
b >>= 1; // b=b/2;
}
return ans;
}