[题解]SP20173 DIVCNT2 - Counting Divisors (square)

· · 题解

分析

我们先定义前缀和算子:

\mathrm Sf(n)=\sum_{i=1}^nf(i)

设 \delta(n)=\mathrm d(n^2),那么本题就是求 \mathrm S\delta(n)。

传统艺能,考虑贝尔级数:

F_p(x)=\sum_{n \ge 0}f(p^n)x^n

那么:

\begin{aligned} \Delta_p(x)&=\sum_{n \ge 0}\delta(p^n)x^n\\ &=\sum_{n \ge 0}\mathrm d(p^{2n})x^n\\ &=\sum_{n \ge 0}(2n+1)x^n\\ &=2\sum_{n \ge 0}n^{\underline 1}x^n+\sum_{n \ge 0}x^n\\ &=2\sum_{n \ge 0}\dbinom{n}{1}x^n+\sum_{n \ge 0}x^n\\ &=2x\sum_{n \ge 0}\dbinom{n+1}{1}x^n+\sum_{n \ge 0}x^n\\ &=\dfrac{2x}{(1-x)^2}+\dfrac{1}{1-x}\\ &=\dfrac{1+x}{(1-x)^2}\\ &=(1+x)\left(\dfrac{1}{1-x}\right)\left(\dfrac{1}{1-x}\right) \end{aligned}

所以 \delta=\mu^2*\mathbf 1*\mathbf 1=\mu^2*\mathrm d。

在数论分块与杜教筛求狄利克雷卷积/逆元前缀和中都用到了这个众所周知的式子:

\mathrm S(f*g)(n)=\sum_{i=1}^nf(i)\mathrm Sg\left(\left\lfloor\dfrac{n}{i}\right\rfloor\right)=\sum_{i=1}^ng(i)\mathrm Sf\left(\left\lfloor\dfrac{n}{i}\right\rfloor\right)

证明显然,暴力拆即可。

那么我们只需要求出 \mathrm S\mathrm d 和 \mathrm S\mu^2 在所有 \left\lfloor\dfrac{n}{i}\right\rfloor 的 \Theta(\sqrt n) 个点值,就可以 \Theta(\sqrt n) 数论分块了。

\mathrm S\mathrm d

这个比较显然。由于 \mathrm d=\mathbf 1*\mathbf 1,直接套前面的式子即可:

\mathrm S\mathrm d(n)=\sum_{i=1}^n\left\lfloor\dfrac{n}{i}\right\rfloor

事实上它等价于这个式子:

-\lfloor\sqrt n\rfloor^2+\sum_{i=1}^{\lfloor\sqrt n\rfloor}\left\lfloor\dfrac{n}{i}\right\rfloor

这种方法常数比数论分块略小。

证明贺这篇文章的一张图(这篇文章里也给出了代数证明):

\mathrm S\mu^2

就是 P4318。以下解法是从这里贺来的。

我们设 f(n)=\sqrt{\max\limits_{d^2 \mid n}d},那么:

\begin{aligned} \mathrm S\mu^2(n)&=\sum_{i=1}^n\mu^2(n)\\ &=\sum_{i=1}^n[f(i)=1]\\ &=\sum_{i=1}^n\sum_{d \mid f(i)}\mu(d)\\ &=\sum_{i=1}^n\sum_{d^2 \mid f^2(i)}\mu(d) \end{aligned}

不难证明 d^2 \mid f^2(n) \iff d^2 \mid n:

充分性:由于 f^2(n) \mid n,由传递性可知 d^2 \mid n。

必要性:设 n 的唯一分解形式为 p_1^{k_1}p_2^{k_2}\cdots p_m^{k_m},那么 f(n)=p_1^{\mathrm r(k_1)}p_2^{\mathrm r(k_2)}\cdots p_m^{\mathrm r(k_m)},其中 \mathrm r(n)=2\left\lfloor\dfrac{n}{2}\right\rfloor。由于 n 的平方因子中 p_i 的次数一定都是小于等于 k_i 的偶数,所以 n 的每个平方因子一定是 f(n) 的因子。

代入进去:

\begin{aligned} \mathrm S\mu^2(n)&=\sum_{i=1}^n\sum_{d^2 \mid i}\mu(d)\\ &=\sum_{i=1}^n\sum_{d=1}^{\lfloor\sqrt n\rfloor}\mu(d)[d^2 \mid i]\\ &=\sum_{d=1}^{\lfloor\sqrt n\rfloor}\mu(d)\sum_{i=1}^n[d^2 \mid i]\\ &=\sum_{d=1}^{\lfloor\sqrt n\rfloor}\mu(d)\left\lfloor\dfrac{n}{d^2}\right\rfloor\\ \end{aligned}

复杂度

显然求 \mathrm S\mathrm d 和 \mathrm S\mu^2 的复杂度都是 \Theta(\sqrt n),因此套上分块后的复杂度为 \Theta\left(n^{\frac{3}{4}}\right),比较危险。

和杜教筛一样,考虑预处理到 N。那么复杂度为:

\begin{aligned} &\Theta(N)+T\sum_{i=1}^{\sqrt n}\left[\dfrac{n}{i} \ge N\right]\sqrt{\dfrac{n}{i}}\\ \asymp\;&N+T\sum_{i=1}^{\sqrt n}\left[i \le \dfrac{n}{N}\right]\sqrt{\dfrac{n}{i}}\\ =\;&N+T\sum_{i=1}^{\frac{n}{N}}\sqrt{\dfrac{n}{i}}\\ \sim\;&N+T\int_0^{\frac{n}{N}}\sqrt{\dfrac{n}{i}}\\ =\;&N+\dfrac{2Tn}{\sqrt N}\\ \asymp\;&N+\dfrac{Tn}{\sqrt N}\\ \end{aligned}

显然当加号两边相等时复杂度最小。因此:

\begin{aligned} N&=\dfrac{Tn}{\sqrt N}\\ N^{\frac{3}{2}}&=Tn\\ N&=(Tn)^{\frac{2}{3}} \end{aligned}

然后就可以做了。时间复杂度 \Theta\left((Tn)^{\frac{2}{3}}\right)。

代码

#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <queue>
#include <bitset>
#include <algorithm>
typedef unsigned int uint;
//typedef long long ll;
typedef unsigned long long ull;
typedef ull ll;

template <typename T>
inline T max(T a, T b){return (a>b) ? a : b;}
template <typename T>
inline T min(T a, T b){return (a<b) ? a : b;}

template <typename T>
void read(T &x){
    static char ch;
    x = 0;
    while (!isdigit(ch=getchar()));
    do x = (x<<3)+(x<<1)+(ch^'0');
    while (isdigit(ch=getchar()));
}

template <typename T>
void write(T x, char c = ' '){
    static char st[100], *p;
    if (x < 0) putchar('-'), x = -x;
    if (x == 0) putchar('0');
    for (p = st; x; x /= 10) *++p = x%10;
    for (; p != st; --p) putchar(*p|'0');
    putchar(c);
}

template <typename T>
inline void writeln(T x){write(x, '\n');}

constexpr uint MAXN = 1e8+7;
uint N;
bool isp[MAXN];
int primes[MAXN];
int mu[MAXN];
uint c[MAXN], mu2[MAXN], d[MAXN];

void init(){ // 线性筛预处理
    memset(isp, 1, sizeof isp);
    uint cnt = 0;
    mu[1] = d[1] = 1;
    for (uint i = 2; i <= N; ++i){
        if (isp[i]) primes[cnt++] = i, mu[i] = -1, c[i] = 1, d[i] = 2;
        for (uint j = 0; j < cnt && i*primes[j] <= N; ++j){
            isp[i*primes[j]] = 0;
            if (!(i%primes[j])){
                mu[i*primes[j]] = 0, c[i*primes[j]] = c[i]+1;
                d[i*primes[j]] = d[i]/(c[i]+1)*(c[i]+2); break;
            }
            mu[i*primes[j]] = -mu[i], c[i*primes[j]] = 1, d[i*primes[j]] = d[i]<<1;
        }
    }
    for (uint i = 1; i <= N; ++i) mu2[i] = (mu[i]>=0)?mu[i]:-mu[i];
    for (uint i = 2; i <= N; ++i) mu2[i] += mu2[i-1];
    for (uint i = 2; i <= N; ++i) d[i] += d[i-1];
}

ll sum_mu2(ll n){
    if (n <= N) return mu2[n];
    ll ans = 0;
    for (ll i = 1; i*i <= n; ++i) ans += n/(i*i)*mu[i];
    return ans;
}

ll isqrt(ll n){ // 牛顿迭代求整数平方根
    ll x = 1; bool flag = 0;
    for (;;){
        ll nx = (x+n/x)>>1;
        if (x==nx || (nx>x && flag)) break;
        flag = nx<x;
        x = nx;
    }
    return x;
}

ll sum_d(ll n){
    if (n <= N) return d[n];
    ll ans = 0, s = isqrt(n);
    for (ll i = 1; i <= s; ++i) ans += n/i;
    return (ans<<1)-s*s;
}

ll solve(ll n){
    ll ans = 0, pre = 0, res;
    for (ll l = 1, r; l <= n; l = r+1){
        r = n/(n/l);
        ans += ((res=sum_d(r))-pre)*sum_mu2(n/l), pre = res;
    }
    return ans;
}

ll data[10005];

int main(){
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    int t; ll m = 0;
    read(t); for (int i = 0; i < t; ++i) read(data[i]), m += data[i];
    N = min((uint)pow(m, 2.0/3.0), MAXN); init();
    for (int i = 0; i < t; ++i) writeln(solve(data[i]));


    return 0;
}