[洛谷P4213]【模板】杜教筛(Sum)
题目大意:给你$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)的更多相关文章
- 洛谷P4213(杜教筛)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...
- p4213 【模板】杜教筛(Sum)
传送门 分析 我们知道 $\varphi * 1 = id$ $\mu * 1 = e$ 杜教筛即可 代码 #include<iostream> #include<cstdio> ...
- [模板] 杜教筛 && bzoj3944-Sum
杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...
- luoguP4213 [模板]杜教筛
https://www.luogu.org/problemnew/show/P4213 同 bzoj3944 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...
- LG4213 【模板】杜教筛(Sum)和 BZOJ4916 神犇和蒟蒻
P4213 [模板]杜教筛(Sum) 题目描述 给定一个正整数$N(N\le2^{31}-1)$ 求 $$ans_1=\sum_{i=1}^n\varphi(i)$$ $$ans_2=\sum_{i= ...
- 51NOD 1222 最小公倍数计数 [莫比乌斯反演 杜教筛]
1222 最小公倍数计数 题意:求有多少数对\((a,b):a<b\)满足\(lcm(a,b) \in [1, n]\) \(n \le 10^{11}\) 卡内存! 枚举\(gcd, \fra ...
- 洛谷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 ...
- P4213 【模板】杜教筛(Sum)
\(\color{#0066ff}{题 目 描 述}\) 给定一个正整数\(N(N\le2^{31}-1)\) 求 \(\begin{aligned} ans_1=\sum_{i=1}^n\varph ...
- P4213【模板】杜教筛(Sum)
思路:杜教筛 提交:\(2\)次 错因:\(\varphi(i)\)的前缀和用\(int\)存的 题解: 对于一类筛积性函数前缀和的问题,杜教筛可以以低于线性的时间复杂度来解决问题. 先要构造\(h= ...
随机推荐
- rust学习
Rust (github) 1. install (https://rustup.rs/) 2. play on line curl https://sh.rustup.rs -sSf | sh e ...
- (转载)golang 整数常量INT_MAX INT_MIN最大值最小值
转载地址:https://blog.csdn.net/bdss58/article/details/78388858 在C语言中,有标准库limits.h定义了一些最大最小值常量,例如int类型的最大 ...
- TCP Keepalive笔记
TCP是无感知的虚拟连接,中间断开两端不会立刻得到通知.一般在使用长连接的环境下,需要心跳保活机制可以勉强感知其存活.业务层面有心跳机制,TCP协议也提供了心跳保活机制. 长连接的环境下,人们一般使用 ...
- Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)
public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...
- easyui datagrid怎么动态获取表头的列名及显示名称
说明:目前使用easyui combobox多选属性,绑定的数据源是来自datagrid的表头的列名及显示名称 处理方法: //获取冻结的数据源并返回key,value格式数据 var GetFroz ...
- WebGL学习笔记(四):绘图
图元 WebGL可以绘制非常复杂的3D模型,这些模型都是由下面3种基本几何图元构成的,下面我们来详细的看看. 三角形 WebGL中任何复杂的模型,都是由三角形组合而成的,可以说三角形是任意形状的最小构 ...
- 转 zabbix debug and zabbix使用percona插件监控mysql
########## https://www.cnblogs.com/keithtt/p/8542987.html zabbix使用percona插件监控mysql 1.添加percona仓库. ...
- Excel统计发票和金税盘核对新版
之前的博文:如何使用Excel表格状态栏动态查看统计,介绍了如何利用excel一拉就可以进行统计,和金税盘的月度统计统计.由于最近年月日显示成方框,所以作废了发票和对冲了上月的一张发票,导致这个月出现 ...
- LIST<>泛型集合取得对象的属性值
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- ue4 优化建议与经验
转自:https://dawnarc.com/2016/12/ue4%E4%BC%98%E5%8C%96%E5%BB%BA%E8%AE%AE%E4%B8%8E%E7%BB%8F%E9%AA%8C/ 内 ...