Kattis - exponial

本文介绍了一道名为Kattis-exponial的问题,该问题要求计算n(n−1)(n−2)⋅⋅⋅n^{(n-1)^{(n-2)^{···}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kattis - exponial

题意:
n(n1)(n2) %m。

思路:
欧拉降幂公式。
ab=ab%ϕ(n)+ϕ(n)(mod n)
n<5 时要特判。

代码:

#include<bits/stdc++.h>
#define ll long long

ll phi(ll x) //欧拉函数
{
    ll res=1;
    for(ll p=2;p*p<=x;++p)
    {
        if(x%p==0)
        {
            res*=p-1;
            x/=p;
        }
        while(x%p==0)
        {
            res*=p;
            x/=p;
        }
    }
    if(x>1)
    {
        res*=x-1;
    }
    return res;
}

ll _pow(ll a,ll b,ll m) //快速幂 %m
{
    ll res=1;
    while(b)
    {
        if(b&1) res=(res*a)%m;
        b/=2;
        a=(a*a)%m;
    }
    return res;
}

ll expo_trunc(ll n)
{
    return n<4?n<3?n:9:100000;
}

ll solve(ll n,ll m) //降幂公式
{
    if(m==1) return 0;
    if(n==1) return 1;
    ll om=phi(m),e=expo_trunc(n-1);
    if(e==100000) e=om+solve(n-1,om);
    return _pow(n,e,m);
}

int main()
{
    ll n,m;
    while(~scanf("%lld %lld",&n,&m))
    {
        printf("%lld\n",solve(n,m));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值