LP3934 Nephren Ruq Insania [扩展欧拉定理/树状数组]

Author Avatar
空気浮遊 2018年07月24日
  • 在其它设备中阅读本文章
  • 数论
  • 树状数组
  • 扩展欧拉定理
  • 分享到 Facebook
  • 分享到 Telegram
  • 分享到 Twitter
  • 分享到微博

emm

Problem

https://www.luogu.org/problemnew/show/P3934

Solution

扩展欧拉定理:
$$a^b\equiv \begin{cases} &a^{b\%\varphi(p)} &\gcd(a,p)=1\\ &a^b &\gcd(a,p)\neq1,b<\phi(p)\\ &a^{b\%\varphi(p)+\varphi(p)} &\gcd(a,p)\neq1,b\geq\phi(p) \end{cases}\pmod p$$

可以证明如此递归 mod 下去,模数会在 $\log$ 层的递归之后变为 1。因此暴力递归计算是 $\log n$ 级的。

但是我们可能并不知道上面的 $b$ 是否是大于 $\phi(p)$ 的。

我们发现假设我们递归到 $l$ 层,往后看五个数。如果中间出现了 $1$,则终点到达。

否则至少会是 $2^{2^{2^{2^{2}}}}>>p$。

因此稍微超前算一下就行了。

复杂度 $\text{O( 玄学)}$。

Solution

// Code by ajcxsu
// Problem: nephren

#include<bits/stdc++.h>
using namespace std;

template<typename T> void gn(T &x) {
    char ch=getchar();
    x=0;
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar();
}

const int N=5e5+10, M=2e7+10;
typedef long long ll;
ll pri[M], p, phi[M];
bool npri[M];


#define lowbit(x) x&-x
ll C[N];
void updata(int x, ll d) {
    while(x<N) {
        C[x]+=d;
        x+=lowbit(x);
    }
}
ll query(int x) {
    ll ret=0;
    while(x) {
        ret+=C[x];
        x-=lowbit(x);
    }
    return ret;
}

ll qpow(ll x, ll y, ll mo) {
    ll ret=1;
    x%=mo;
    while(y) {
        if(y&1ll) ret=(ret*x) % mo;
        x=(x*x) % mo, y>>=1ll;
    }
    return ret;
}

ll cac(int l, int r, ll mo) {
    if(mo==1) return 0;
    if(l==r) return query(r)%mo;
    int f=min(l+5, r);
    for(int i=l+1;i<=f;i++) if(query(i)==1) f=i;
    ll last=query(f), q=0;
    for(int i=f-1;i>l;i--) {
        q=last, last=1;
        while(q--) {
            last*=query(i);
            if(last>=phi[mo]) return qpow(query(l), cac(l+1,r,phi[mo])+phi[mo], mo);
        }
    }
    return qpow(query(l), last, mo);
}

int main() {
    phi[1]=1;
    for(ll i=2;i<M;i++) {
        if(!npri[i]) pri[p++]=i, phi[i]=i-1;
        for(int j=0;j<p && i*pri[j]<M;j++) {
            npri[i*pri[j]]=1;
            if(i%pri[j]==0) { phi[i*pri[j]]=phi[i]*pri[j]; break; }
            else phi[i*pri[j]]=phi[i]*(pri[j]-1);
        }
    }
    int n, m;
    gn(n), gn(m);
    int c, l, r;
    ll x;
    for(int i=1;i<=n;i++) gn(x), updata(i, x), updata(i+1, -x);
    while(m--) {
        gn(c), gn(l), gn(r), gn(x);
        if(c==1) updata(l, x), updata(r+1, -x);
        else printf("%lld\n", cac(l, r, x));
    }
    return 0;
}

本文链接:https://pst.iorinn.moe/archives/sol-luogu-3934.html
许可: https://pst.iorinn.moe/license.html
若无特别说明,博客内的文章默认将采用 CC BY 4.0 许可协议 进行许可☆

      新篇
旧篇      
    • fingerprint Login
  • home 主页
  • inbox 归档
    • June 2026 1
    • December 2024 1
    • August 2024 1
    • June 2024 1
    • April 2024 3
    • March 2024 1
    • February 2024 2
    • January 2024 1
    • November 2023 1
    • August 2023 1
    • May 2023 1
    • February 2023 2
    • January 2023 2
    • July 2022 1
    • June 2022 1
    • April 2022 1
    • March 2022 1
    • February 2022 2
    • December 2021 1
    • November 2021 1
    • August 2021 3
    • July 2021 3
    • April 2021 2
    • March 2021 1
    • February 2021 4
    • January 2021 2
    • December 2020 1
    • November 2020 1
    • October 2020 3
    • September 2020 1
    • August 2020 1
    • July 2020 1
    • March 2020 1
    • December 2019 1
    • September 2019 1
    • July 2019 1
    • April 2019 2
    • March 2019 13
    • February 2019 15
    • January 2019 11
    • December 2018 3
    • November 2018 6
    • October 2018 28
    • September 2018 31
    • August 2018 18
    • July 2018 13
    • June 2018 27
    • May 2018 11
    • April 2018 12
    • March 2018 19
    • February 2018 8
    • January 2018 7
    • December 2017 2
    • November 2017 2
  • apps 分类
    • 笔记
    • 题解
    • 杂文
    • 技术
    • 游戏
    • 小说
  • 留言板
  • 关于
  • 友链
  • 文章总数 281
主题 - Material i
expand_less
Copyright © 2026 雪屋
Float in air.
Powered by Typecho
Theme - Material