17D 扩展欧拉函数

求(b-1)*(b)^(n-1)%c,b和n都是百万位数
b和b-1都能直接取模
两种方法:
一:问题在于降幂
a^b %c= a^(b%phi(c)+phi(c)) %c (b>=phi(c))
if(phi(c)>b) 直接 a^b%c.

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
ll Eular(ll n)
{
    ll ans=n;
    for(int i=2;i*i<=n;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 quick_pow(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=a*ans%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
char b[1000005],n[1000005];
int main()
{//(b-1)*b^(n-1)%c
    ll C;
    scanf("%s%s%lld",b,n,&C);
    int LenB=strlen(b),LenN=strlen(n);
    ll B=0,N=0;
    for(int i=0;i<LenB;i++)
        B=(B*10LL+b[i]-'0')%C;//B=b%c
    ll Phi=Eular(C);
    for(int i=0;i<LenN;i++)
        N=(N*10LL+n[i]-'0')%Phi;//N=N%phi(c);
    ll ans=B-1LL+C;
    N=(N-1LL+Phi)%Phi+Phi;//(N-1)%phi(c)+phi(c);
    if(LenN<=10)
    {
        ll RealN=0LL;
        for(int i=0;i<LenN;i++) RealN=RealN*10LL+n[i]-'0';
        if(RealN<Phi) N-=Phi;
    }
    ans=(ans*quick_pow(B,N,C))%C;
    if(!ans) ans=C;
    printf("%lld\n",ans);
}

二:
2^735=((2^7)^10*2^3)^10*2^5
百万位,只需要for循环100万次解决
a^b%c ,a和b存在数组中

    ll bb=0,ans=1;
    for(int i=0;i<LenB;i++)
        bb=(bb*10LL+b[i]-'0')%C;
    for(int i=0;i<=lenn-1;i++)
    {
        ans=quick_pow(ans,10);
        ans=ans*pow1(bb,n[i]-'0');
        ans%=c;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值