【数论】[codevs 2952 细胞分裂]快速幂

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<assert.h>
using namespace std;
typedef unsigned long long LL;
LL p,q;
int prime[]={2,3,5,7,11,13,17,19,23,29};
/*慢速乘法*/ 
LL mul(LL a,LL b)
{
	LL ans = 0;
	for(LL i=a;i;i>>=1)
	{
		if(i&1)ans = (ans + b)%p;
		b = (b+b)%p;
	}
	return ans%p;
}
/*快速幂 */
LL mul1(LL a,LL b)
{
	LL ans = 1;
	for(LL i=a;i;i>>=1)
	{
		if(i&1)ans = mul(ans,b)%p;
		b = mul(b,b)%p;
	}
	return ans%p;
}
int main()
{
	cin>>q>>p;
	cout<<mul1(q,2);
	return 0;
}

你可能感兴趣的:(算法,数论,快速幂)