\(\color{#0066ff}{题 目 描 述}\)

给定一个正整数\(N(N\le2^{31}-1)\)

\(\begin{aligned} ans_1=\sum_{i=1}^n\varphi(i) \end{aligned}\)

\(\begin{aligned} ans_2=\sum_{i=1}^n \mu(i) \end{aligned}\)

\(\color{#0066ff}{输 入 格 式}\)

一共T+1行

第1行为数据组数T(T<=10)

第2~T+1行每行一个非负整数N,代表一组询问

\(\color{ #0066ff }{ 输 出 格 式 }\)

一共T行,每行两个用空格分隔的数ans1,ans2

\(\color{#0066ff}{输入样例}\)

6
1
2
8
13
30
2333

\(\color{#0066ff}{ 输 出 样 例}\)

1 1
2 0
22 -2
58 -3
278 -3
1655470 2

\(\color{#0066ff}{数 据 范 围 与 提 示}\)

\(N \leq 2^{31}\)

\(\color{#0066ff}{题 解}\)

前置知识1 : 狄利克雷卷积

对于任意函数f,g,有\(\begin{aligned} h(i) = \sum_{d|i}f(d)*g(\frac{n}{d})\end{aligned}\)

h即为f和g的卷积

常用函数

1、\(i(n) = 1\)

2、\(id(n) = n\)

3、\(e(n)=\left\{\begin{aligned}1\ \ \ n = 1 \\ 0 \ \ \ n \neq 1\end{aligned}\right.\)

4、欧拉函数\(\varphi(n)\)

5、懵逼钨丝函数\(\mu(n)=\left\{\begin{aligned}1\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n = 1 \\ (-1)^k \ \ \ n由k个不同质数相乘得到\\ 0\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 其它情况\end{aligned}\right.\)

6、\(\sigma(n)=n的约数和\)

7、\(d(n)=n的约数个数\)

常用卷积

1、\(i*\mu = e\)

2、\(e*a=a\)

3、\(\mu * id= \varphi\)

4、\(i*id=\sigma\)

5、\(i*i=d\)

6、\(i*\varphi=id\)

杜教筛

已知\(f(i)\)

用来求\(\begin{aligned}\sum_{i = 1}^n f(i)\end{aligned},n\leq 2^{31}\)

定义\(h(i)=(f*g)(i)=\begin{aligned}\sum_{d|i}f(d)*g(\frac{i}{d})\end{aligned}\)

\(\displaystyle\sum_{i=1}^nh(i)\)

用定义展开

\(=\displaystyle\sum_{i=1}^n\sum_{d|i}g(d)f\left(\frac i d\right)\)

d的范围也是【1.n】的,所以改成枚举d,找它的倍数,这个式子是在求和,找全了就行

\(=\displaystyle \sum_{d=1}^ng(d)\sum_{d|i}f\left(\frac i d \right)\)

把后面变一下

\(=\displaystyle \sum_{d=1}^ng(d)\sum_{i=1}^{\left\lfloor\frac n d \right \rfloor}f( i)\)

然后

\(=\displaystyle \sum_{i=1}^ng(i)S\left(\left\lfloor\frac n i\right\rfloor\right)\)

所以

\(\displaystyle \sum_{i=1}^nh(i)=\sum_{i=1}^ng(i)S\left(\left\lfloor\frac n i\right\rfloor\right)\)

有一个好像没用的式子

\(\displaystyle g(1)S(n)=\sum_{i=1}^ng(i)S\left(\left\lfloor\frac n i\right\rfloor\right)-\sum_{i=2}^ng(i)S\left(\left\lfloor\frac n i\right\rfloor\right)\)

上式把后面移项就成恒等式了

我们把右面第一项用刚刚的结论换走

\(\displaystyle g(1)S(n)=\sum_{i=1}^nh(i)-\sum_{i=2}^ng(i)S\left(\left\lfloor\frac n i\right\rfloor\right)\)

这。。是个递归式

就没了

对于S的递归,用数列分块

一般的h和g都很好求(构造)

对于本题来说

\(i*\varphi=id\)

所以对于\(\varphi\)

\(\displaystyle S(n)=\frac{n*(n+1)}{2}-\sum_{i=2}^nS\left(\left\lfloor\frac n i\right\rfloor\right)\)

刚刚有\(i*\mu=e\)

所以

\(\displaystyle S(n)=1-\sum_{i=2}^nS\left(\left\lfloor\frac n i\right\rfloor\right)\)

没了。。。

把前\(4*10^6\)的东西线性筛一下

最后的复杂度\(O(n^{\frac{2}{3}})\)不会证

#include <bits/stdc++.h>

typedef long long LL;

const int maxn = 4e6;
const int maxx = 4e6 + 10; int in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x * f;
} bool vis[maxx];
LL phi[maxx];
int mu[maxx], pri[maxx], tot;
std::map<int, LL> P;
std::map<int, int> M; void predoit() {
phi[1] = mu[1] = 1LL;
for(int i = 2; i <= maxn; i++) {
if(!vis[i]) {
pri[++tot] = i;
phi[i] = i - 1;
mu[i] = -1;
}
for(int j = 1; j <= tot && i * pri[j] <= maxn; j++) {
vis[i * pri[j]] = true;
if(i % pri[j] == 0) {
phi[i * pri[j]] = phi[i] * pri[j];
mu[i * pri[j]] = 0;
break;
}
else {
phi[i * pri[j]] = phi[i] * (pri[j] - 1);
mu[i * pri[j]] = -mu[i];
}
}
}
for(int i = 2; i <= maxn; i++) {
phi[i] += phi[i - 1];
mu[i] += mu[i - 1];
}
} LL workphi(int now)
{
if(now <= maxn) return phi[now];
if(P.count(now)) return P[now];
LL ans = now * (now + 1LL) / 2;
for(int i = 2, lst; i <= now; i = lst + 1) {
lst = now / (now / i);
ans -= 1LL * (lst - i + 1LL) * workphi(now / i);
}
return P[now] = ans;
} int workmu(int now)
{
if(now <= maxn) return mu[now];
if(M.count(now)) return M[now];
int ans = 1;
for(int i = 2, lst; i <= now; i = lst + 1) {
lst = now / (now / i);
ans -= workmu(now / i) * (lst - i + 1);
}
return M[now] = ans;
} int main() {
predoit();
for(int T = in(); T --> 0;) {
int n = in();
printf("%lld %d\n", workphi(n), workmu(n));
}
return 0;
}

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

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

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

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

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

  3. luoguP4213 [模板]杜教筛

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

  4. 洛谷P4213(杜教筛)

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

  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$,求:$$\sum\limits_{i=1}^n\varphi(i),\sum\limits_{i=1}^n\mu(i)$$最多$10$组数据,$n\leqslant2^{31}- ...

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

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

  9. BZOJ3944: Sum(杜教筛模板)

    BZOJ3944: Sum(杜教筛模板) 题面描述 传送门 题目分析 求\(\sum_{i=1}^{n}\mu(i)\)和\(\sum_{i=1}^{n}\varphi(i)\) 数据范围线性不可做. ...

随机推荐

  1. 1104 Sum of Number Segments

    题意: 给出n个不大于1.0的小数序列,如{ 0.1, 0.2, 0.3, 0.4 },则共有10个分片(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, ...

  2. Python类(四)-多态

    多态即一个接口,多种实现 按照平常直接调用 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obje ...

  3. navicat for mysql ,mysql版本是8.0的版本,连接数据库报错1251,解决办法。

    我的mysql版本是8.0的版本,因为毕竟新的mysql采用新的保密方式,所以就的似乎不能用,改密码方式: 用管理员身份打开cmd mysql -uroot -p(输入密码)            进 ...

  4. 环境变量,include搜索路径,lib库搜索路径

    环境变量 系统环境变量 我们知道,我们经常要设置一些环境变量,系统环境变量我们非常容易理解.其实我们在windows中经常容易接触.其实环境变量是一个非常广泛的一个概念,它与web应用程序中的web. ...

  5. 两种布局的ListVIew Adapter。例如微信对话界面

    这个界面  实现的不是微信对话界面.实现的是,focus的状态下,变为放大的另一种布局 重点: 一.定义类型个数 private final int TYPE_COUNT = 2;    privat ...

  6. ubuntu16部署gitlab

    一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...

  7. day70 12-存储过程和存储函数

    什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...

  8. winform 对话框控件

    ColorDialog 可以调节颜色的控件,如果给一个按钮点击事件 ColorDialog.showdialog();就会弹出这个 返回值是个枚举类 然后定义一个这个类的变量 接收一下它的返回值 Di ...

  9. 算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令

    package algorithms.util; /************************************************************************** ...

  10. CF570E Pig and Palindromes

    完全不会这种类型的$dp$啊…… 考虑回文串一定是可以拆分成(偶数个字母 + 偶数个字母)或者(偶数个字母 + 一个字母 +偶数个字母),两边的偶数个字母其实是完全对称的.因为这道题回文串的长度是给定 ...