FZU 1759-Super A^B mod C(快速幂+大整数取模+欧拉函数)

题目链接:点击打开链接

题意:计算 a^b %c 但其中b很大,可能会达到10^1000000, 故有降幂公式 a^b %c= a^(b%phi(c)+phi(c)) %c  (b>=phi(c)) 

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 10100
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define pp pair<int,int>
#define ull unsigned long long
using namespace std;
int a,c;char b[1000010];
ll phi(ll n)
{
	ll m=(ll)sqrt(n+0.5),ans=n;
	for(ll i=2;i<=m;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(n%i==0)n/=i;
		}
	}
	if(n>1)ans=ans/n*(n-1);
	return ans;
}
ll pow_mod(ll a,ll n,ll p)
{
	if(n==0)return 1;
	ll ans=pow_mod(a,n/2,p);
	ans=ans*ans%p;
	if(n&1)ans=ans*a%p;
	return ans;
}
void solve()
{
	int len=strlen(b);ll tem=phi(c),sb;
	if(len<=10)
	{
		sscanf(b,"%I64d",&sb);
		if(sb>=tem)
			printf("%I64d\n",pow_mod(a,sb%tem+tem,c));
		else
			printf("%I64d\n",pow_mod(a,sb,c));
		return ;
	}
	ll ans=0;
	for(int i=0;i<len;i++)
		ans=(ans*10+(b[i]-'0'))%tem;
	printf("%I64d\n",pow_mod(a,ans+tem,c));
}
int main()
{
	while(~scanf("%I64d %s %I64d",&a,b,&c))
		solve();
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值