A simple Blog for wyx I've been down the bottle hoping.
4916: 神犇和蒟蒻 杜教筛 狄利克雷卷积
发表于: | 分类: Oi | 评论:0 | 阅读:62

第一问不会的出门左转百度$\mu$是啥去

第二问的话显然答案是等于$\sum_{i=1}^n{i\varphi(i)}$的,不知道的出门左转百度$\varphi$计算公式去……

然后考虑那个东西我们用杜教筛搞一下

考虑$f(x)=x$和$g(x)=x\varphi(x)$的狄利克雷卷积$G(x)=\sum_{d|x}f(\frac{x}{d})g(d)$

首先我们可以直接带入一下

$G(t)=\sum_{x=1}^t \sum_{d|x}\frac{x}{d}d\varphi(d)=\sum_{d|x}x\varphi(d)$

$G(t)=\sum_{x=1}^t x\sum_{d|x}\varphi(d)$

注意到后面的东西其实是$x$

所以其实$G(t)=\sum_{x=1}^t {x^2} = \frac{t(t+1)(2t+1)}{6}$

然后考虑我们不带入然后算贡献

$G(t)=\sum_{x=1}^t \sum_{d|x}f(d)g(\frac{x}{d})$

$=\sum_{x=1}^t f(x)\sum_{x|d}^n g(\frac{d}{x})$这步的话其实就是相当于我枚举了一个数和乘积然后算另外一个的贡献

$=\sum_{x=1}^t \sum_{k=1}^{\frac{n}{d}}g(k)$

考虑当$x=1$的时候后面的东西就是$g(n)$即为所求,剩下的根号分块递归处理即可

最后令$S(x)=\sum_{i=1}^n i\varphi(i)$

那么会有$S(n) = G(i) - \sum_{i=2}^n i S(\frac{n}{i})$

手写了$hash$表并没有变快qwq

// 631887325
#include <map>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5e6+5;
const int Max = 5e6;
const int inv2 = 500000004;
const int inv6 = 166666668;
const int mod = 1e9+7;
const int mod1 = 5e6+5;
const int M = 5 * mod1;
int prime[N], cnt, phi[N], head[mod1+3];
bool F[N];
map <int,int> mp;

struct graph {
    int next, to, val;
    graph () {}
    graph (int _next,int _to,int _val)
    :next(_next),to(_to), val(_val){}
}edge[M];

inline void add(int x,int y) {
    int temp = x % mod1;
    edge[++cnt] = graph(head[temp], x, y);
    head[temp] = cnt;
}

inline int find(int x) {
    int temp = x % mod1;
    for(int i=head[temp];i;i=edge[i].next) 
        if(edge[i].to == x) return edge[i].val;
    return -1;
}

inline void init() {
    phi[1] = 1;
    register int i, j;
    for(i=2;i<=Max;++i) {
        if(!F[i]) {
            prime[++cnt] = i;
            phi[i] = i-1;
        }
        for(j=1;prime[j]*i<=Max;++j) {
            F[i*prime[j]] = 1;
            if(i%prime[j]==0) {
                phi[i*prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i*prime[j]] = phi[i] * (prime[j]-1);
        }
    }
    for(i=1;i<=Max;++i) phi[i] = (long long) i*phi[i]%mod;
    for(i=1;i<=Max;++i) (phi[i] += phi[i-1]) %= mod;
}

inline int calc_3(int x) {
    return (long long)x * (x+1) % mod * (2*x+1) % mod * inv6 % mod;
}

inline int calc_2(int x,int y) {
    return (LL)(x+y)%mod*(y-x+1)%mod*inv2%mod;
}

inline int calc(int x) {
    if(x <= Max) return phi[x];
    int tt = find(x);
    if(tt != -1) return tt;
    int last = 0;
    long long ans = 0;
    (ans += calc_3(x)) %= mod;
    for(int i=2;i<=x;i=last+1) {
        last = x/(x/i);
        (ans += mod-(LL)calc(x/i)*calc_2(i,last)%mod) %= mod;
    }
    add(x, ans);
    return ans;
}

int main() {
    init();
    int n;
    cin >> n;
    puts("1");
    cout << calc(n) << endl;
}

Title - Artist
0:00

站点地图 网站地图
Copyright © 2015-2017 A simple Blog for wyx
Powered by Typecho自豪的采用Sgreen主题

TOP