超大数快速幂模板

#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <algorithm>  
#include <string>  
#include <cstdlib>  
using namespace std;  
const int mod = 1e9+7;  
typedef long long ll;  
ll phi(ll n)   //求欧拉函数值   
{  
    int ans=n,temp=n;  
    for(int i=2;i*i<=temp;i++)   
    {  
        if(temp%i==0)   
        {  
            ans-=ans/i;  
            while(temp%i== 0) temp/=i;  
        }  
    }  
    if(temp>1) ans-=ans/temp;  
    return ans;  
}  
ll mod_pow(ll x,ll n,ll mod)  //快速幂   
{     
    ll ans=1;  
    while(n)   
    {  
        if(n%2==1) ans=ans*x%mod;  
        x=x*x%mod;  
        n/=2;  
    }  
    return ans;  
}  
ll a;  
char b[1000010];  
int main()   
{     
    while(scanf("%lld%s",&a,b)!=EOF)   
    {  
        a%=mod;  
        ll phic=phi(mod);  
        int i,len=strlen(b);  
        ll res=0,ans;   
        for( i=0;i<len;i++)
        {
            res=res*10+b[i]-'0';
            if(res>phic)
            break;
        }
        if(i==len)
        {
            ans=mod_pow(a,res,mod)%mod;
        }
        else
        {
            res=0;
            for(int i=0;i<len;i++)
            {
                res=res*10+b[i]-'0';
                res%=phic;
            }
            ans=mod_pow(a,res+phic,mod)%mod;
        }
        cout<<ans<<endl;
    }  
    return 0;  
}

/*当你要计算 A^B%C的时候
因为此题中的B很大,达到10^100000,所以我们应该联想到降幂公式。

降幂公式:A^B%C = A^(B%phi© + phi©)%C
分两种情况:
当B<=phi©时,直接用快速幂计算A^B mod C
当B>phi©时,用快速幂计算A^(B mod phi©+phi©) mod C
*/

#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <algorithm>  
#include <string>  
#include <cstdlib>  
using namespace std;  
const int mod = 1e9+7;  
typedef long long ll;  
ll phi(ll n)   //求欧拉函数值   
{  
    int ans=n,temp=n;  
    for(int i=2;i*i<=temp;i++)   
    {  
        if(temp%i==0)   
        {  
            ans-=ans/i;  
            while(temp%i== 0) temp/=i;  
        }  
    }  
    if(temp>1) ans-=ans/temp;  
    return ans;  
}  
ll mod_pow(ll x,ll n,ll mod)  //快速幂   
{     
    ll ans=1;  
    while(n)   
    {  
        if(n%2==1) ans=ans*x%mod;  
        x=x*x%mod;  
        n/=2;  
    }  
    return ans;  
}  
ll a,c;  
char b[1000010];  
int main()   
{     
    while(scanf("%lld%s%lld",&a,b,&c)!=EOF)   
    {  
        c%=mod;  
        a%=mod;  
        ll phic=phi(mod);  
        int i,len=strlen(b);  
        ll res=0,ans;   
        for( i=0;i<len;i++)
        {
            res=res*10+b[i]-'0';
            if(res>phic)
            break;
        }
        if(i==len)
        {
            ans=mod_pow(a,res,mod)*c%mod;
        }
        else
        {
            res=0;
            for(int i=0;i<len;i++)
            {
                res=res*10+b[i]-'0';
                res%=phic;
            }
            ans=mod_pow(a,res+phic,mod)*c%mod;
        }
        cout<<ans<<endl;
    }  
    return 0;  
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值