Problem 1759 Super A^B mod C (超高次幂)

本文参考自Problem 1759 Super A^B mod C (超高次幂)

查看原文: 原文地址

点击打开链接

Problem Description

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

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

Source

FZU 2009 Summer Training IV--Number Theory

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn=1e6+10;
char B[maxn];

/*
欧拉降幂 
*/

ll A,C;

ll pow_(ll base,ll n){
	ll ans=1,mod=C;
	while(n){
		if(n&1){
			ans=ans*base%mod; 
		}
		base=base*base%mod;
		n>>=1;
	}
	return ans;
}

ll phi(ll x){
	ll ans=x;
	ll m=sqrt(x+0.5);
	for(ll i=2;i<=m;i++){
		if(x%i==0){
			ans=ans-ans/i;
		}
		while(x%i==0)x/=i;
	}
	if(x>1)ans=ans-ans/x;
	return ans;
}


int main()
{
	while(scanf("%I64d %s %I64d",&A,B,&C)==3){
	    ll cc=phi(C);
		int len=strlen(B);
		ll bb=0;
		if(len<=15){
			for(int i=0;i<len;i++)
			  bb=bb*10+B[i]-'0';
		}
		else{
			//大整数 mod 
			for(int i=0;i<len;i++){
				bb=(bb*10+B[i]-'0')%cc; 
			}
			bb+=cc;	
		}
		ll ans=pow_(A,bb);
		printf("%I64d\n",ans);
	}
	return 0;
}

本文参考自Problem 1759 Super A^B mod C (超高次幂)

查看原文: 原文地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值