fzu Problem 1759 Super A^B mod C(快速幂取模+欧拉函数)

题目链接:传送门

乍看之下有点像普通的快速幂取模 但是给的B的值太大  所以是根本无法解决的。。。

但是 有一个公式 a^x mod(c)=a^(x mod phi(c) +phi(c)) mod(c) (x>=phi(c))

相关介绍请看AC大神 传送门

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

char ch[MAXN];
ll Euler(ll x)
{
    ll res=x;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            res=res-res/i;
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        res=res-res/x;
    return res;
}

ll quickpow(ll m,ll n,ll k)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=((ans%k)*(m%k))%k;
        n>>=1;
        m=((m%k)*(m%k))%k;
    }
    return ans;
}

int main()
{
//    fread;
    ll a,b,c;
    while(scanf("%I64d",&a)!=EOF)
    {
        scanf("%s",ch);
        scanf("%I64d",&c);
        int len=strlen(ch);
        ll phi=Euler(c);
        if(len<10)
        {
            b=ch[0]-'0';
            for(int i=1;i<len;i++)
            {
                b*=10;
                b+=ch[i]-'0';
            }
            if(b<phi)
            {
                printf("%I64d\n",quickpow(a,b,c));
                continue;
            }
        }
        b=0ll;
        for(int i=0;i<len;i++)
        {
            b*=10;
            b+=ch[i]-'0';
            while(b>=phi)
                b-=phi;
        }
        printf("%I64d\n",quickpow(a,b,c));
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值