FZU1752&1650--A^B mod C--快速幂取模

本文介绍了快速幂运算和模运算的基本概念,并提供了一个解决此类问题的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     Problem Description

Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63).

Input

There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.

Output

For each testcase, output an integer, denotes the result of A^B mod C.

Sample Input

3 2 42 10 1000

Sample Output

124

---------------------------------------------------------
分析:1.a*b mod c = ((a mod c)*(b mod c)) mod c = ((a mod c)*b) mod c
             (a + b) mod c = ((a mod c)+( b mod c)) mod c
           2.必须使用快速幂
           3.必须注意数值范围(1<=A,B,C<2^63),这恰好是long long 的范围(-2^63~2^63-1),但是又怎么保证C在没取模之前的范围在long long之内?无法保证。所以定义范围应该比long long 更大,所以使用的是unsigned long long(0~2^64-1)。
          4.尽管使用了unsigned long long类型,但是在A累乘的时候还是会溢出unsigned long long的范围.所以应该将乘法运算变为加法防止范围的溢出
              
#include<iostream>
#include<cstdio>
#define LL unsigned long long
using namespace std;
LL mod; 
LL mulit(LL x,LL y){
	LL ans = 0;
	while(y){
		if(y&1){
			ans += x;
			if(ans >= mod) 
			    ans -= mod;
		}
		x <<= 1;//x = x + x = 2 * x; 
		if(x >= mod) x -= mod;
		y >>= 1;
	}
	return ans;
} 
LL quickpow(LL x,LL n) {
	LL ans = 1;
	x = x%mod;
	while(n){
		if(n&1){//判断n是否为奇数 
			ans = mulit(ans,x);
		}
		x = mulit(x,x);
		n >>= 1;//相当于n/2 
	}
	return ans;
}
int main(){
	LL a,b;
	while(~scanf("%I64u%I64u%I64u",&a,&b,&mod)){//注意出入格式,或者直接用cin输入,cout输出,避免格式问题 
		printf("%I64u\n",quickpow(a,b));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值