2019icpc 南京网络赛b

https://nanti.jisuanke.com/t/41299

指数循环节,快速幂递归求解,需要注意的是快速幂乘的时候如果超过了m需要+m补全循环节

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const ll inf=2147483647;

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

ll eular(ll n)
{
    ll ans = n;
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            ans -= ans / i;
            while (n % i == 0)
                n /= i;
        }
    }
    if (n != 1)
        ans -=ans / n;
    return ans;
}

inline ll md(ll a,ll m)
{
    return a>=m ? a%m+m:a;    //超过m就+m补全循环节
}

int quick_mi(ll a, ll k,ll p)
{
    ll ans = 1;
    while (k)
    {
        if (k & 1)
            ans=md(ans*a,p);
        a=md(a*a,p);
        k >>= 1;
    }
    return ans;
}


int dfs(int x,int y,int p)
{
    if (y == 0)
        return 1;
    if (y == 1)
        return x;
    if (p == 1)
        return 1;
    ll ans=quick_mi(x,dfs(x,y-1,eular(p)),p);
    return ans;
}

int main()
{
    int t;
    t=read();
    while(t--)
    {
        int a,b,m;
        a=read(),b=read(),m=read();
        int ans=dfs(a,b,m);
        cout<<ans%m<<endl;
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/youchandaisuki/p/11452593.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值