Super A^B mod C FZU - 1759 (欧拉降幂)

Given A,B,C, You should quickly calculate the result of ABmodC A B m o d C . (1<=A,C<=1000000000,1<=B<=101000000). ( 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 4
2 10 1000
Sample Output
1
24

思路:因为幂的数量级很大,肯定不能直接算。用欧拉定理降幂就行,前几天遇到过个类似的题,这个算是一般化了。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
long long phi(long long x)
{
    long long ans=x;
    for(long long i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        ans=ans/x*(x-1);
    return ans;
}
long long get(long long a,long long b,long long mod)
{
    long long ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
long long a,c;
char b[1000005];
int main()
{
    while(scanf("%lld%s%lld",&a,b,&c)==3)
    {
        long long P=phi(c);
        long long left=0;
        int len=strlen(b);
        for(int i=0;i<len;i++)
        {
            left=left*10+b[i]-'0';
            if(left>P)
                left%=P;
        }
        cout<<get(a,left,c)<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值