题目描述
快速幂板子题
今天没事写一些题解,以前忙的时候老是草草复制一下别人的代码就提交了,于心不安。
样例
听闻远方有她,从此执剑天涯。
C++ 代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int p=233333;
int ksm(int x,int n,int p)
{
int res=1;
while(n){
if(n&1) res=(ll)res*x%p;//满足条件即让res带走一次x,实现x的倍增,这是快速幂算法的核心。
x=(ll)x*x%p;//x孤独的自乘等待下一次res的救赎。
n>>=1;//每次二进制指数项部分右移1。
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int x,n;
cin>>x>>n;
int res=ksm(x,n,p);
cout<<res;
return 0;
}