题目大意:给你$n$,求:
$$
\sum\limits_{i=1}^n\varphi(i),\sum\limits_{i=1}^n\mu(i)
$$
最多$10$组数据,$n\leqslant2^{31}-1$

题解:杜教筛,用来求$\sum\limits_{i=1}^nf(i)$的,其中$f$是某个特殊函数。

若我们可以找到一个函数$g$,使得$g,f*g$两个函数的前缀和十分好算($g*f$表示$g$和$f$的狄利克雷卷积),就可在$O(n^{\frac 23})$的复杂度内求出我们要的东西。令$S(n)=\sum\limits_{i=1}^nf(i)$
$$
\begin{align*}
\sum\limits_{i=1}^n(g*f)(i)&=\sum\limits_{i=1}^n\sum\limits_{d|i}g(d)f\left(\dfrac id\right)\\
&=\sum\limits_{d=1}^ng(d)\sum\limits_{i=1,d|i}^nf\left(\dfrac id\right)\\
&=\sum\limits_{d=1}g(d)S\left(\left\lfloor\dfrac nd\right\rfloor\right)
\end{align*}\\
g(1)S(n)=\sum\limits_{i=1}^n(f*g)(i)-\sum\limits_{i=2}^ng(i)S\left(\left\lfloor\dfrac ni\right\rfloor\right)
$$
然后线性筛求出前$n^{\frac23}$项,剩余的递归+整除分块计算,需要记忆化。

$$
\sum\limits_{i=1}^n\varphi(i):\\
\because\varphi*I=id\\
\therefore S(n)=\frac{n(n+1)}{2}-\sum_{i=2}^nS\left(\left\lfloor\dfrac n i\right\rfloor\right)
$$
$$
\sum\limits_{i=1}^n\mu(i):\\
\because\mu*I=1\\
\therefore S(n)=1-\sum_{i=2}^nS\left(\left\lfloor\frac n i\right\rfloor\right)
$$

注意这道题中$n\leqslant2^{31}-1$,$+1$后会爆$int$,所以整除分块的时候要用$unsigned$

卡点:

C++ Code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <unordered_map>
const int R = 1 << 22 | 1;
int plist[R >> 3], ptot;
bool notp[R];
long long phi[R], mu[R];
void sieve() {
phi[1] = mu[1] = 1;
for (int i = 2; i < R; ++i){
if (!notp[i]) plist[ptot++]=i, phi[i] = i - 1, mu[i] = -1;
for(int j = 0, t; (t = i * plist[j]) < R; ++j){
notp[t] = true;
if(i % plist[j] == 0) {
phi[t] = phi[i] * plist[j];
mu[t] = 0;
break; }
phi[t] = phi[i] * phi[plist[j]];
mu[t] = -mu[i];
}
}
for (int i = 2; i < R; ++i) phi[i] += phi[i - 1], mu[i] += mu[i - 1];
}
std::unordered_map<unsigned, long long> Phi, Mu;
long long sphi(unsigned n) {
if (n < R) return phi[n];
if (Phi.count(n)) return Phi[n];
long long &t = Phi[n];
for (unsigned l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
t += (r - l + 1) * sphi(n / l);
}
return t = static_cast<long long> (n + 1) * n / 2 - t;
}
long long smu(unsigned n) {
if (n < R) return mu[n];
if (Mu.count(n)) return Mu[n];
long long &t = Mu[n];
for (unsigned l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
t += (r - l + 1) * smu(n / l);
}
return t = 1 - t;
} int T;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cin.tie(0);
sieve();
std::cin >> T;
while (T --> 0) {
static unsigned n;
std::cin >> n;
std::cout << sphi(n) << ' ' << smu(n) << '\n';
}
return 0;
}

  

[洛谷P4213]【模板】杜教筛(Sum)的更多相关文章

  1. 洛谷P4213(杜教筛)

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...

  2. p4213 【模板】杜教筛(Sum)

    传送门 分析 我们知道 $\varphi * 1 = id$ $\mu * 1 = e$ 杜教筛即可 代码 #include<iostream> #include<cstdio> ...

  3. [模板] 杜教筛 && bzoj3944-Sum

    杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...

  4. luoguP4213 [模板]杜教筛

    https://www.luogu.org/problemnew/show/P4213 同 bzoj3944 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...

  5. LG4213 【模板】杜教筛(Sum)和 BZOJ4916 神犇和蒟蒻

    P4213 [模板]杜教筛(Sum) 题目描述 给定一个正整数$N(N\le2^{31}-1)$ 求 $$ans_1=\sum_{i=1}^n\varphi(i)$$ $$ans_2=\sum_{i= ...

  6. 51NOD 1222 最小公倍数计数 [莫比乌斯反演 杜教筛]

    1222 最小公倍数计数 题意:求有多少数对\((a,b):a<b\)满足\(lcm(a,b) \in [1, n]\) \(n \le 10^{11}\) 卡内存! 枚举\(gcd, \fra ...

  7. 洛谷P4213 Sum(杜教筛)

    题目描述 给定一个正整数N(N\le2^{31}-1)N(N≤231−1) 求ans_1=\sum_{i=1}^n\phi(i),ans_2=\sum_{i=1}^n \mu(i)ans1​=∑i=1 ...

  8. P4213 【模板】杜教筛(Sum)

    \(\color{#0066ff}{题 目 描 述}\) 给定一个正整数\(N(N\le2^{31}-1)\) 求 \(\begin{aligned} ans_1=\sum_{i=1}^n\varph ...

  9. P4213【模板】杜教筛(Sum)

    思路:杜教筛 提交:\(2\)次 错因:\(\varphi(i)\)的前缀和用\(int\)存的 题解: 对于一类筛积性函数前缀和的问题,杜教筛可以以低于线性的时间复杂度来解决问题. 先要构造\(h= ...

随机推荐

  1. 虚拟变量和独热编码的区别(Difference of Dummy Variable & One Hot Encoding)

    在<定量变量和定性变量的转换(Transform of Quantitative & Qualitative Variables)>一文中,我们可以看到虚拟变量(Dummy Var ...

  2. 55、Spark Streaming:updateStateByKey以及基于缓存的实时wordcount程序

    一.updateStateByKey 1.概述 SparkStreaming 7*24 小时不间断的运行,有时需要管理一些状态,比如wordCount,每个batch的数据不是独立的而是需要累加的,这 ...

  3. Automatic Ship Detection in Optical Remote Sensing Images Based on Anomaly Detection and SPP-PCANet

    基于异常检测和 PCANet 的船舶目标检测 船舶检测会遇到三个问题: 1.船低对比度 2.海平面情况复杂 3.云,礁等错误检测 实验步骤: 1.预处理海陆边界,掩膜陆地 2.异常检测获得感兴趣区域, ...

  4. mysql right() 函数

    mysql> ); +---------------------+ | right() | +---------------------+ | dedede | +--------------- ...

  5. 【POJ3083】Children of the Candy Corn

    本题传送门 本题知识点:深度优先搜索 + 宽度优先搜索 本题题意是求三个路径长度,第一个是一直往左手边走的距离,第二个是一直往右手边走的距离,第三个是最短距离. 第三个很好办,就是一个简单的bfs的模 ...

  6. 【cf补题记录】A. Hotelier

    思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Hotelier 题意 给出长度为n的字符串,字符串由'L'.'R'以及数字0~9组成.旅馆有10间房子,L代表客人从左边入住,R代表客人从右边入 ...

  7. Linux下的nexus数据迁移

    刚到公司没多久,目前公司有两个项目公用一个nexus的maven私服,现在想把两个私服的jar包拆分开: 我在原私服的nexus服务器中, 1.备份原nexus使用命令 完成tar包的压缩 打包完毕后 ...

  8. magento2重写virtualType并且传参

    今天遇到一个需求需要重写一个block,但是这个block是应用virtualType实现,所以需要先重写virtualType,然后却因为参数丢失而获取不到正确的结果.因此,查阅文档,需要用type ...

  9. ThreadLocal:的面试

    ThreadLocal: 为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变 ...

  10. Java 动态调试技术原理及实践 【基本功】Java动态追踪技术探究

    https://mp.weixin.qq.com/s/ZlNcvwJ_swspifWTLHA92Q https://mp.weixin.qq.com/s/_hSaI5yMvPTWxvFgl-UItA