P4213【模板】杜教筛(Sum)
思路:杜教筛
提交:\(2\)次
错因:\(\varphi(i)\)的前缀和用\(int\)存的
题解:
对于一类筛积性函数前缀和的问题,杜教筛可以以低于线性的时间复杂度来解决问题。
先要构造\(h=f*g\),并且\(h\)的前缀和易求,\(g\)的区间和易求。
具体地:
\]
设\(S(n)\)表示\(\sum_{i=1}^{n}f(i)\)
\]
\]
当我们对后面的式子进行整除分块时,求\(S(n)\)的复杂度为\(O(n^{\frac{2}{3}})\)
所以主要就是如何构造\(h\)和\(g\)
好吧直接说了:
\(\epsilon=\mu\cdot I\)
\(id=\varphi\cdot I\)
对于\(f(n)=\varphi(n)\cdot n^k=\varphi(n^{k+1})\)的一类方法:
\(id^{k+1}=f\cdot id^k\)
#include<cstdio>
#include<iostream>
#include<unordered_map>
#include<cmath>
#define ll long long
#define R register int
using namespace std;
namespace Luitaryi {
template<class I> inline I g(I& x) { x=0; register I f=1;
register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f;
} const int N=5000000,Inf=2147483647;
int T,n,cnt,p[N/4],mu[N+10];
ll phi[N+10];
bool v[N+10];
inline void PRE() { phi[1]=mu[1]=1;
for(R i=2;i<=N;++i) {
if(!v[i]) p[++cnt]=i,phi[i]=i-1,mu[i]=-1;
for(R j=1;j<=cnt&&i*p[j]<=N;++j) {
v[i*p[j]]=true;
if(i%p[j]==0) {
mu[i*p[j]]=0;
phi[i*p[j]]=phi[i]*p[j]; break;
} mu[i*p[j]]=-mu[i];
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
for(R i=1;i<=N;++i) mu[i]+=mu[i-1];
for(R i=1;i<=N;++i) phi[i]+=phi[i-1];
}
unordered_map<int,int> memmu;
unordered_map<int,ll> memphi;
inline ll s_phi(int n) {
if(n<=N) return phi[n];
if(memphi.count(n)) return memphi[n];
register ll ans=0;
for(R l=2,r;r<Inf&&l<=n;l=r+1) {
r=n/(n/l),ans+=(r-l+1)*s_phi(n/l);
} return memphi[n]=1llu*n*(n+1ll)/2ll-ans;
}
inline int s_mu(int n) {
if(n<=N) return mu[n];
if(memmu.count(n)) return memmu[n];
register ll ans=0;
for(R l=2,r;r<Inf&&l<=n;l=r+1) {
r=n/(n/l),ans+=(r-l+1)*s_mu(n/l);
} return memmu[n]=1ll-ans;
}
inline void main() {
PRE(); g(T); while(T--) {
g(n); printf("%lld %d\n",s_phi(n),s_mu(n));
}
}
} signed main() {Luitaryi::main(); return 0;}
2019.08.23
77
P4213【模板】杜教筛(Sum)的更多相关文章
- 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 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...
- 洛谷P4213(杜教筛)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...
- 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$,求:$$\sum\limits_{i=1}^n\varphi(i),\sum\limits_{i=1}^n\mu(i)$$最多$10$组数据,$n\leqslant2^{31}- ...
- P4213 【模板】杜教筛(Sum)
\(\color{#0066ff}{题 目 描 述}\) 给定一个正整数\(N(N\le2^{31}-1)\) 求 \(\begin{aligned} ans_1=\sum_{i=1}^n\varph ...
- BZOJ3944: Sum(杜教筛模板)
BZOJ3944: Sum(杜教筛模板) 题面描述 传送门 题目分析 求\(\sum_{i=1}^{n}\mu(i)\)和\(\sum_{i=1}^{n}\varphi(i)\) 数据范围线性不可做. ...
随机推荐
- Feign【@FeignClient】
首先看一下@FeignClient注解的源码: package org.springframework.cloud.openfeign; import java.lang.annotation.Doc ...
- PAT甲级 排序题_C++题解
排序题 PAT (Advanced Level) Practice 排序题 目录 <算法笔记> 6.9.6 sort()用法 <算法笔记> 4.1 排序题步骤 1012 The ...
- 下载GDB调试工具peda
命令: 1.git clone https://github.com/longld/peda.git ~/peda 2.echo "source ~/peda/peda.py" & ...
- Python【Network/XHR/json】
##################################################################### 制定一个目标(爬取周杰伦的歌曲清单): 根据目标,确认一个方 ...
- ASP.NET Core 过滤器
继承Attribute,IActionFilter实现自己的过滤器类,并且在Startup,mvc服务中注入. 全局都会过滤,在任意一个controller,action执行前和执行后都会过滤一次 通 ...
- 如何在 arm 官网上找到合适的手册
http://infocenter.arm.com/help/advanced/help.jsp 在这里输入合适的版号即可 这样就可以不用去 CSDN 了 100000_0000_00_EN - AR ...
- 2019杭电多校一 K. Function (数论)
大意: 给定$n(n\le 10^{21})$, 求$\sum\limits_{i=1}^n gcd(\lfloor\sqrt[3]{i}\rfloor,i)\mod 998244353$ 首先立方根 ...
- hdu 4501三重包问题
好好理解一下背包问题 从01包入手 内层的循环 是为了以后求解记录数据 因为只有一个取舍问题 所以只需要一层循环就可以 这里有三个背包 钱 积分 以及免费物品 那么 就需要三重循环 #include& ...
- (十三)使用handler实现登录验证
一.Handel概念 J2EE Web 服务中的Handler技术特点非常像Servlet技术中的Filter.我们知道,在Servlet中,当一个HTTP到达服务端时,往往要经过多个Filter对请 ...
- Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'
An unhandled exception occurred while processing the request. InvalidOperationException: Unable to r ...