一道codevs的模板题:
codevs3500
题目:快速幂入门
时间限制: 1 s
空间限制: 1000 KB
题目等级 : 白银 Silver
题解
查看运行结果
题目描述 Description
输入3个数a,b,c,求a^b mod c=?
输入描述 Input Description
三个数a,b,c
输出描述 Output Description
一个数,即a^b mod c 的答案。
样例输入 Sample Input
5 10 9
样例输出 Sample Output
4
数据范围及提示 Data Size & Hint
0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
long long ans=1,n,m,a,b,c,pd,x=1,p=0;
using namespace std;
void quick()
{
//a=a%c;千万不可以有这一步,血淋淋的教训
while(b)
{
if(b%2==1) ans=(ans*a)%c;//加入这一位对应二进制为1,就乘上 if语句写成if(b&1) 也行
b>>=1;//相当于 b=b/2;
a=(a*a)%c;
}
}
int main()
{
scanf("%lld%lld%lld",&a,&b,&c);
quick();
printf("%lld",ans%c);
return 0;
}
.
.
.
.
.
.
模板:
void quick(int a,int b,int c)
{
while(b)
{
if(b&1) ans=(ans*a)%c;
b>>=1;
a=(a*a)%c;
}
}