码迷,mamicode.com
首页 > 其他好文 > 详细

模板—十进制快速幂

时间:2019-07-15 09:12:39      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:进制   ret   情况   cst   while   signed   name   bsp   sig   

用于指数爆longlong的情况,当然你也可以打高精……

因为昨天有人提到了慢速乘,感觉挺有用的,就顺便都学了吧,而且省选也用到十进制快速幂了。

#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
char c[100000];
LL a,p,t;
LL tenthpow(LL a)
{
	LL ans=1,s=a;
	while(t>=0)
	{
		LL cnt=c[t]-‘0‘,cur=s;
		for(int i=1;i<=cnt;i++)
		ans=ans*s%p;
		for(int i=1;i<10;i++)
		cur=cur*s%p;
		s=cur;ans%=p;t--;
	}
	return ans;
}
signed main()
{
	cin>>a>>c>>p;
	t=strlen(c);t--;
	cout<<tenthpow(a)<<endl;
}

 

模板—十进制快速幂

标签:进制   ret   情况   cst   while   signed   name   bsp   sig   

原文地址:https://www.cnblogs.com/Al-Ca/p/11186778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!