P7513 Solution

· · 题解

update:已补充完整证明。

本题解提供了一种 \Theta(\sqrt[4]{a}+\sqrt p+k\omega(a))(其中 \omega(a) 表示 a 的质因子个数)的整式递推做法,显著优于 std。

至于其中的 \sqrt p 是因为要做到以 \Theta(k) 的时间复杂度计算 k 个同底的快速幂,需要预处理。

为了大家看的方便,这里试着尽量把式子完整推一遍。

根据单位根的基本性质

\prod_{i=1}^{n}(x-\omega_n^i)=x^n-1

代入 x=-1 就得到

\prod_{i=1}^n(1+\omega_n^i)=(-1)^n((-1)^n-1) = 2\times(n\bmod 2)

由于连乘中只要有一项为零,答案就是零,所以 n 为偶数时答案就是零。

若 n 为奇数,设答案

\prod_{x_1=1}^n\prod_{x_2=1}^n\cdots\prod_{x_t=1}^n\left( 1+\exp \left( \frac{2\pi \text i}{n}\prod_{i=1}^t x_i\right) \right)

而 g(n,t)=\log_2f(n,t)。

考虑递推,把 \exp 中乘积的 x_1 提出来,设 d=\gcd(x_1,n) 这样就能约分:

f(n,t)=\prod_{x_1=1}^n\prod_{x_2=1}^n\cdots\prod_{x_t=1}^n\left( 1+\exp \left( \frac{2\pi \text i x_1/d}{n/d}\prod_{i=2}^t x_i\right) \right)

根据单位根的另一个性质,当 n,k 互质时有:

\prod_{i=0}^{n-1}(x-\omega_n^{ki})=\prod_{i=0}^{n-1}(x-\omega_n^i)=x^n-1

这是因为 ki\bmod n \ (i\in[0,n-1]) 可以取遍所有 [0,n-1] 的整数。

根据这个性质,前面式子中的 x_1/d 就可以直接去掉,对答案没有影响。

f(n,t)= \prod_{x_1=1}^n\prod_{x_2=1}^n\cdots\prod_{x_t=1}^n\left( 1+\exp \left( \frac{2\pi \text i }{n/d}\prod_{i=2}^t x_i\right) \right) =\prod_{x_1=1}^{n}\left(\prod_{x_2=1}^{n/d}\cdots\prod_{x_t=1}^{n/d}\left( 1+\exp \left( \frac{2\pi \text i }{n/d}\prod_{i=2}^t x_i\right) \right)\right)^{d^{t-1}} =\prod_{x_1=1}^nf(n/d,t-1)^{d^{t-1}}

也即

g(n,t)=\sum_{i=1}^n g\left(\frac{n}{\gcd(n,i)},t-1\right)\gcd(n,i)^{t-1} =\sum_{d|n}g\left(\frac{n}{d},t-1\right)d^{t-1}\sum_{i=1}^n [\gcd(n,i)=d] =\sum_{d|n} g( d,t-1)\varphi(d)\left(\frac nd \right)^{t-1}

根据最开始的推论,我们知道 g(n,1)=1,关于 n 是积性函数,故 g(n,t) 关于 n 也是积性函数,只需对输入的 a 质因数分解,计算 g(p^m,t) 的值即可。

g(p^m,t)=\sum_{i=0}^{m}g(p^i,t-1)\varphi(p^i)p^{(m-i)(t-1)}

不要忘了 \varphi(p^k)=(p-1)p^{k-1} 仅在 k\geq 1 时成立。

g(p^m,t)= p^{(t-1)m-1}+\frac{p-1}{p}\sum_{i=0}^m g(p^i,t-1)p^i\times p^{(t-1)(m-i)}

关于答案的式子个人没想到比较简洁的证法,暂时把原题解的搬过来,到时候再补
完整证明已经出炉,速戳 \to Link \leftarrow

g(p^m,t)= \frac{m^{\overline t}p^{(t-1)m}}{(t-1)!}\sum_{j=0}^{t-1} \binom {t-1}{j} \frac{(-p^{-1})^j}{m+j}

注意到这个和式实际上是一个二项卷积,抛开看到卷积就用 FFT 处理的思维定势,我们可以考虑下面这个式子如何递推计算,原问题就能迎刃而解:

a_n=\sum_{i=0}^{n} \binom {n}i \frac{q^i}{m+i}

设 \mathcal A(x) 为 \{ a_n\}_{n=0}^{\infty} 的 EGF,\mathcal H(x) 为 \left\{ \dfrac{q^n}{m+n} \right\}_{n=0}^\infty 的 EGF,就有

\mathcal A(x) = \text e^x\mathcal H(x)

根据一点整式递推的知识,\mathcal H(x) 的系数可以整式递推,\text e^x 的系数显然也可以整式递推,所以 \mathcal A(x) 的系数也可以整式递推。

进一步地,我们知道

(m+n)h_n=q^n

将其用微分方程表示,注意 EGF 中的第 n 项都要除以 n!。

m \frac{h_n}{n!}+\frac{h_n}{(n-1)!}=\frac{q^n}{n!} m\mathcal H(x)+x\mathcal H'(x)=\text e^{qx}

代入 \mathcal A(x) 的计算式:

\mathcal A'(x) =\text e^x(\mathcal H(x)+\mathcal H'(x)) \mathcal A'(x) =\mathcal A(x)+\text e^x\mathcal H'(x) \mathcal x\mathcal A'(x)=x\mathcal A(x)+\text e^x\left( \text e^{qx}-m\mathcal H(x)\right) x\mathcal A'(x)=(x-m)\mathcal A(x)+\text e^{(q+1)x}

推到这一步就可以直接提系数了:

na_n=na_{n-1}-ma_n+ (q+1)^n

直接做就能以 \Theta(n) 的时间复杂度求出前 n 项。

最后就是细节部分,求 g(n,t) 需要对 335544322 取模,做 CRT 化为对 2 和另一个大质数取模。

由 g(n,t) 的初始递推式知道答案一定是奇数,在模 2 下可以直接出结果。
最后别忘了分解 a 的质因数还需要 Pollard-Rho。

参考代码: (感谢 LinearODE 提供的 pollard-rho 模板)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<ctime>
#define ll long long
#define reg register
#define N 200003
#define p 167772161
#define md 335544323
#define i128 __int128_t
using namespace std;

inline int power(int a,int t){
    int res = 1;
    while(t){
        if(t&1) res = (ll)res*a%p;
        a = (ll)a*a%p;
        t >>= 1;
    }
    return res;
}

ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }

inline ll poww(ll a,ll t,ll m){
    ll res = 1;
    while(t){
        if(t&1) res = (i128)res*a%m;
        a = (i128)a*a%m;
        t >>= 1;
    }
    return res;
}

inline bool miller_rabin(ll x,ll b){
    ll k = x-1;
    while(k){
        ll cur = poww(b,k,x);
        if(cur!=1&&cur!=x-1) return false;
        if((k&1)||cur==x-1) return true;
        k >>= 1;
    }
    return true;
}

inline bool prime(ll x){
    if(x==46856248255981ll||x<2) return false;
    if(x==2||x==3||x==7||x==61||x==24251) return true;
    return miller_rabin(x,2)&&miller_rabin(x,61);
}

inline ll pollard_rho(ll x){ // 由于我不会 pr,这段是蒯来的板子
    ll s = 0,t = 0,c = (rand()*rand()+rand())%(x-1)+1;
    for(int goal=1;;goal<<=1,s=t){
        ll val = 1;
        for(int stp=1;stp<=goal;++stp){
            t = ((i128)t*t+c)%x;
            val = (i128)val*abs(t-s)%x;
            if(stp%127==0){
                ll d = gcd(val,x);
                if(d>1) return d;
            }
        }
        ll d = gcd(val,x);
        if(d>1) return d;
    }
    return -1;
}

ll max_factor;

inline void find_factor(ll x){
    if(x<=max(max_factor,2ll)) return;
    if(prime(x)){
        max_factor = max(max_factor,x);
        return;
    }
    ll d = x;
    while(d>=x) d = pollard_rho(x);
    while(x%d==0) x /= d;
    find_factor(x);
    find_factor(d);
}

int fac[N],ifac[N];

void init(int n){
    fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
    for(int i=2;i<=n;++i) fac[i] = (ll)fac[i-1]*i%p;
    ifac[n] = power(fac[n],p-2);
    for(int i=n-1;i;--i) ifac[i] = (ll)ifac[i+1]*(i+1)%p;
}

inline int binom(int n,int m){
    if(n<m) return 0;
    return (ll)fac[n]*ifac[m]%p*ifac[n-m]%p;
}

int ans[N];
int k;

void solve(int pr,ll m){ // 整式递推部分
    static int pre[N],suf[N],a[N];
    int q = p-power(pr,p-2),mul,upf = 1,pt = power(pr,m%(p-1));
    int _m = m%p;
    a[0] = power(_m,p-2);
    pre[0] = suf[k+1] = 1;
    for(int i=1;i<=k;++i) pre[i] = (ll)pre[i-1]*(i+_m)%p;
    for(int i=k;i;--i) suf[i] = (ll)suf[i+1]*(i+_m)%p;
    mul = power(suf[1],p-2);
    for(int i=1;i<=k;++i){
        upf = (ll)upf*(q+1)%p;
        a[i] = ((ll)i*a[i-1]%p+upf)*mul%p*pre[i-1]%p*suf[i+1]%p;
    }
    upf = q = 1;
    for(int i=1;i<=k;++i){
        upf = (ll)upf*(_m+i-1)%p;
        ans[i] = (ll)ans[i]*a[i-1]%p*upf%p*q%p*ifac[i-1]%p;
        q = (ll)q*pt%p;
    }
}

ll a,b,cnt;
int low[1<<15],high[12003];

int main(){
    srand(time(0));
    int m,xx,out = 0;
    scanf("%lld%lld%d",&a,&b,&k);
    if(!(a&1)){
        putchar('0');
        return 0;
    }
    init(k+3);
    for(int i=0;i<=k;++i) ans[i] = 1;
    while(a>1){
        max_factor = cnt = 0;
        find_factor(a);
        while(a%max_factor==0) a /= max_factor,++cnt;
        solve(max_factor%p,b*cnt);
    }
    for(int i=1;i<=k;++i) ans[i] = (p+(ll)ans[i]*(p+1))%(md-1); // crt
    low[0] = high[0] = 1;
    for(int i=1;i!=32768;++i) low[i] = (low[i-1]<<1)>=md?(low[i-1]<<1)-md:(low[i-1]<<1);
    m = (low[32767]<<1)%md;
    for(int i=1;i!=12000;++i) high[i] = (ll)high[i-1]*m%md;
    for(int i=1;i<=k;++i){
        xx = (ll)low[ans[i]&32767]*high[ans[i]>>15]%md;
        out ^= xx;
    }
    printf("%d",out);
    return 0;
}

个人隐约觉得这个做法还可以继续优化,即做到 \Theta(\sqrt[4]a + \sqrt p + k) 的时间复杂度,但不知可行与否。

如果有懂哥的话还请指点一下(