super_log

super_log

In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So in practical application, we often assume α(n) \le 4α(n)≤4.

super_log 链接

##欧拉降幂公式

不用考虑是否互质,直接运用公式,逐渐将其迭代

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int maxn=1000020;
const int Maxn=1000020;
ll prime[Maxn],phi[Maxn],pn=0;
bool jud[Maxn];
ll quickjia(ll a,ll b,ll p)
{
    ll  sum=0;
    while(b)
    {
        if(b&1)
        {
            sum=(sum+a)%p;
        }
        a=(a+a)%p;
        b>>=1;
    }
    return sum;
}
ll quick(ll a,ll b,ll p)
{
    ll sum=1;
    while(b)
    {
        if(b&1)
        {
            sum=quickjia(sum,a,p)%p;
        }
        a=quickjia(a,a,p)%p;
        b>>=1;
    }
    return sum;
}
void init()//欧拉函数打表
{
    phi[1]=1;
    for(int i=2; i<=maxn; i++)
    {
        if(!jud[i])
        {
            prime[++pn]=i;
            phi[i]=i-1;
        }
        for(int j=1; j<=pn; j++)
        {
            ll cnt=i*prime[j];
            if(cnt>maxn)
                break;
            jud[cnt]=true;
            if(i%prime[j]==0)
            {
                phi[cnt]=phi[i]*prime[j];
                break;
            }
            else
            {
                phi[cnt]=phi[i]*(prime[j]-1);
            }
        }
    }
}
ll slove(ll a,ll b,ll p)
{
    if(p==1)
        return 0;
    if(b==0)
        return 1;
    ll s=slove(a,b-1,phi[p]);
    if(s<phi[p]&&s)
        return quick(a,s,p);
    else//注意零的特判,零直接当做指数。
        return quick(a,s+phi[p],p);
}
int main()
{
    int t;
    init();
    scanf("%d",&t);
    ll a,b,m;
    ll ans=0;
    while(t--)
    {
        scanf("%lld %lld %lld",&a,&b,&m);
        ans=slove(a,b,m);
        printf("%lld\n",ans%m);
    }

}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值