【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接:
1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239
1244:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244
杜教筛裸题,不过现在我也只会筛这俩前缀和...
$$s(n)=\sum _{i=1}^{n}f(i)$$
那么就有:
$$\sum_{i=1}^{n}f(i)\lfloor \frac{n}{i} \rfloor=\sum_{i=1}^{n}s(\lfloor \frac{n}{i} \rfloor)=s(n)+\sum_{i=2}^{n}s(\lfloor \frac{n}{i} \rfloor)$$
移项得到:
$$s(n)=\sum_{i=1}^{n}f(i)\lfloor \frac{n}{i} \rfloor-\sum_{i=2}^{n}s(\lfloor \frac{n}{i} \rfloor)$$
对于欧拉函数,$f(n)=\phi(n)$
$$\sum_{i=1}^{n}\phi(i)\lfloor \frac{n}{i} \rfloor=\sum_{i=1}^{n}\sum_{d|n}\phi(d)=\sum_{i=1}^{n}i=\frac{n*(n+1)}{2}$$
对于莫比乌斯函数,$f(n)=\mu(n)$
$$\sum_{i=1}^{n}\mu(i)\lfloor \frac{n}{i} \rfloor=\sum_{i=1}^{n}\sum_{d|n}\mu(d)=\sum_{i=1}^{n}[i=1]=1$$
然后这两个公式就可以在线筛预处理$n^{\frac{2}{3}}$只后记忆化达到$O(n^{\frac{2}{3}})$的效率.
值得注意的就是,记忆化要写hash,以及不要忘了取模,筛欧拉函数前缀和时牵扯取模和除2,可以先讨论奇偶除掉2再计算。
1239:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
#define N 5000000
#define P 233333
#define MAXN 250000
#define MO 1000000007
int cnt,prime[N+10],flag[N+10];
LL X,phi[N+10];
inline void Pre(LL n)
{
flag[1]=1; phi[1]=1;
for (LL i=2; i<=n; i++)
{
if (!flag[i]) prime[++cnt]=i,phi[i]=i-1;
for (int j=1; j<=cnt && i*prime[j]<=n; j++)
{
flag[i*prime[j]]=1;
if (!(i%prime[j])) {phi[i*prime[j]]=phi[i]*prime[j]; break;}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for (LL i=1; i<=n; i++) phi[i]=(phi[i]+phi[i-1])%MO;
}
struct Hash{
int next; LL i,x;
}mp[MAXN];
int head[MAXN],tot;
inline void Add(LL i,LL x) {int pos=i%P; tot++; mp[tot].next=head[pos]; head[pos]=tot; mp[tot].i=i; mp[tot].x=x;}
inline LL Sum(LL x)
{
if (x<=N) return phi[x];
else
{
int pos=x%P;
for (int i=head[pos]; i; i=mp[i].next)
if (mp[i].i==x) {return mp[i].x;}
}
LL sum=0,s=0;
for (LL i=2,j; i<=x; i=j+1)
j=x/(x/i),(sum+=Sum(x/i)%MO*(j-i+1)%MO)%=MO;
if (x&1) s=(((x+1)/2)%MO)*(x%MO)%MO; else s=((x/2)%MO)*((x+1)%MO)%MO;
sum=(s-sum+MO)%MO;
Add(x,sum);
return sum;
}
int main()
{
scanf("%lld",&X);
Pre(N);
printf("%lld\n",Sum(X));
return 0;
}
1244
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
#define P 233333
#define N 5000000
#define MAXN 250000
int cnt,prime[N+10],flag[N+10];
LL L,R,mu[N+10];
inline void Pre(LL n)
{
flag[1]=1; mu[1]=1;
for (LL i=2; i<=n; i++)
{
if (!flag[i]) prime[++cnt]=i,mu[i]=-1;
for (int j=1; j<=cnt && i*prime[j]<=n; j++)
{
flag[i*prime[j]]=1;
if (!(i%prime[j])) {mu[i*prime[j]]=0; break;}
mu[i*prime[j]]=-mu[i];
}
}
for (LL i=1; i<=n; i++) mu[i]+=mu[i-1];
}
struct Hash{
int next; LL i,x;
}mp[MAXN];
int head[MAXN],tot;
inline void Add(LL i,LL x) {int pos=i%P; tot++; mp[tot].next=head[pos]; head[pos]=tot; mp[tot].i=i; mp[tot].x=x;}
inline LL Sum(LL x)
{
if (x<=N) return mu[x];
else
{
int pos=x%P;
for (int i=head[pos]; i; i=mp[i].next)
if (mp[i].i==x) {return mp[i].x;}
}
LL sum=0;
for (LL i=2,j; i<=x; i=j+1)
j=x/(x/i),sum+=Sum(x/i)*(j-i+1);
Add(x,1LL-sum);
return 1LL-sum;
}
int main()
{
scanf("%lld%lld",&L,&R);
Pre(N);
printf("%lld\n",Sum(R)-Sum(L-1));
return 0;
}
【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛的更多相关文章
- 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)
[51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...
- 我也不知道什么是"莫比乌斯反演"和"杜教筛"
我也不知道什么是"莫比乌斯反演"和"杜教筛" Part0 最近一直在搞这些东西 做了将近超过20道题目吧 也算是有感而发 写点东西记录一下自己的感受 如果您真的 ...
- 【BZOJ3930】选数(莫比乌斯反演,杜教筛)
[BZOJ3930]选数(莫比乌斯反演,杜教筛) 题面 给定\(n,K,L,R\) 问从\(L-R\)中选出\(n\)个数,使得他们\(gcd=K\)的方案数 题解 这样想,既然\(gcd=K\),首 ...
- 【BZOJ4652】【NOI2016】循环之美(莫比乌斯反演,杜教筛)
[BZOJ4652]循环之美(莫比乌斯反演,杜教筛) 题解 到底在求什么呢... 首先不管他\(K\)进制的问题啦,真是烦死啦 所以,相当于有一个分数\(\frac{i}{j}\) 因为值要不相等 所 ...
- 【Luogu3768】简单的数学题(莫比乌斯反演,杜教筛)
[Luogu3768]简单的数学题(莫比乌斯反演,杜教筛) 题面 洛谷 \[求\sum_{i=1}^n\sum_{j=1}^nijgcd(i,j)\] $ n<=10^9$ 题解 很明显的把\( ...
- 【LOJ#572】Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛)
[LOJ#572]Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛) 题面 LOJ \[ans=\sum_{i=1}^n\sum_{j=1}^n f(gcd(i,j))^k\ ...
- 【51Nod 1239】欧拉函数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 还是模板题. 杜教筛:\[S(n)=\frac{n(n+1)}{2 ...
- LOJ572. 「LibreOJ Round #11」Misaka Network 与求和 [莫比乌斯反演,杜教筛,min_25筛]
传送门 思路 (以下令\(F(n)=f(n)^k\)) 首先肯定要莫比乌斯反演,那么可以推出: \[ ans=\sum_{T=1}^n \lfloor\frac n T\rfloor^2\sum_{d ...
- 51Nod 1239 欧拉函数前n项和 杜教筛
http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...
- BZOJ4652: [Noi2016]循环之美(莫比乌斯反演,杜教筛)
Description 牛牛是一个热爱算法设计的高中生.在他设计的算法中,常常会使用带小数的数进行计算.牛牛认为,如果在 k 进制下,一个数的小数部分是纯循环的,那么它就是美的.现在,牛牛想知道:对 ...
随机推荐
- 关于new Handler()与new Handler(Looper.getMainLooper())区别
如果你不带参数的实例化:Handler handler=new Handler();那么这个会默认用当前线程的Looper对象. 一般而言,如果你的Handler是要用来刷新UI的,那么就需要在主线程 ...
- tomcat发布html静态页面
一.环境 在Linux系统安装JDK并配置环境变量,安装tomcat(在tomcat官网下载压缩包即可,我使用的是tomcat7 https://tomcat.apache.org/download- ...
- ### mysql系统结构_3_Mysql_Learning_Notes
mysql系统结构_3_Mysql_Learning_Notes 存储层,内存结构 全局(buferpool) 只分配一次 全局共享 连接/会话(session) 针对每个会话/线程分配 按需动态分配 ...
- java四舍五入BigDecimal和js保留小数点两位
java四舍五入BigDecimal保留两位小数的实现方法: // 四舍五入保留两位小数System.out.println("四舍五入取整:(3.856)=" + ne ...
- 【本地服务器】用nodejs搭建最简单、轻量化的http server
1. 引言 前端程序猿主要关注的是页面,你可能根本就用不到.net,java,php等后台语言. 但是你制作出来的网页总要运行.总要测试吧?——那就免不了用到http server.我先前都是用vis ...
- 数据库-mysql视图
视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 一:创建视图 create view view ...
- java基础23 Math类和Random类
一.Math数学类 主要是提供很多数学的公式 1.1.Math类的常用方法 abs(int a):绝对值 ceil(double a):向上取整 floor(double a):向下取整 ...
- java基础20 StringBuffer缓冲类
1.概要 StringBuffer 其实就是一个存储字符的容器 字符串特点:字符串是常量;它们创建之后不能更改了字符串一旦发生变化,那么立马创建一个新的对象.注意:字符串的内容不适合频繁修改的,因为一 ...
- github后端开发面试题大集合(二)
作者:小海胆链接:https://www.nowcoder.com/discuss/3615?type=0&order=0&pos=8&page=0来源:牛客网 7.非关系型数 ...
- python3 安装
Centos7 安装python3 #安装sqlite-devel yum -y install sqlite-devel #安装依赖 yum -y install make zlib zlib-de ...