和bzoj 3944比较像,但是时间卡的更死

设\( f(n)=\sum_{d|n}\mu(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\mu(i) \),然后很显然对于mu\( g(n)=1\),对于\( g(n)=n*(n+1)/2 \),然后可以这样转化一下:

\[g(n)=\sum_{i=1}^{n}\sum_{d|n}\mu(d)
\]

\[=\sum_{d=1}^{n}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor
\]

\[=\sum_{d=1}^{n}s(\left \lfloor \frac{n}{d} \right \rfloor)
\]

\[s(n)=g(n)-\sum_{d=2}^{n}s(\left \lfloor \frac{n}{d} \right \rfloor)
\]

然后递归求解即可。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const long long N=5000005,m=4500000;
long long mb[N],tot,q[N],p[N];
long long l,r;
bool v[N];
long long getp(long long x,long long n)
{
return (x<=m)?mb[x]:p[n/x];
}
void slv(long long x,long long n)
{
if(x<=m)
return;
long long t=n/x;
if(v[t])
return;
v[t]=1;
p[t]=1;
for(long long i=2,la;la<x;i=la+1)
{
la=x/(x/i);
slv(x/i,n);
p[t]-=getp(x/i,n)*(la-i+1);
}
}
long long wk(long long n)
{
if(n<=m)
return mb[n];
memset(v,0,sizeof(v));
slv(n,n);
return p[1];
}
int main()
{
mb[1]=1;
for(long long i=2;i<=m;i++)
{
if(!v[i])
{
q[++tot]=i;
mb[i]=-1;
}
for(long long j=1;j<=tot&&i*q[j]<=m;j++)
{
long long k=i*q[j];
v[k]=1;
if(i%q[j]==0)
{
mb[k]=0;
break;
}
mb[k]=-mb[i];
}
}
for(long long i=1;i<=m;i++)
mb[i]+=mb[i-1];
scanf("%lld%lld",&l,&r);
printf("%lld\n",wk(r)-wk(l-1));
return 0;
}

51nod 1244 莫比乌斯函数之和 【莫比乌斯函数+杜教筛】的更多相关文章

  1. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  2. 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛

    题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...

  3. Wannafly Camp 2020 Day 3D 求和 - 莫比乌斯反演,整除分块,STL,杜教筛

    杜教筛求 \(\phi(n)\), \[ S(n)=n(n+1)/2-\sum_{d=2}^n S(\frac{n}{d}) \] 答案为 \[ \sum_{d=1}^n \phi(d) h(\fra ...

  4. [51nod1237] 最大公约数之和 V3(杜教筛)

    题面 传送门 题解 我好像做过这题-- \[ \begin{align} ans &=\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)\\ &=\sum_{d=1}^ ...

  5. [51nod1238] 最小公倍数之和 V3(杜教筛)

    题面 传送门 题解 懒了--这里写得挺好的-- //minamoto #include<bits/stdc++.h> #define R register #define ll long ...

  6. 51nod 1244 莫比乌斯函数之和 【杜教筛】

    51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...

  7. 51nod 1244 莫比乌斯函数之和(杜教筛)

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

  8. 51Nod.1244.莫比乌斯函数之和(杜教筛)

    题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...

  9. 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛

    题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...

  10. 51 NOD 1244 莫比乌斯函数之和(杜教筛)

    1244 莫比乌斯函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens) ...

随机推荐

  1. Intersection--poj1410(判断线段与矩形的关系)

    http://poj.org/problem?id=1410 题目大意:给你一个线段和矩形的对角两点  如果相交就输出'T'  不想交就是'F' 注意: 1,给的矩形有可能不是左上 右下  所以要先判 ...

  2. Drainage Ditches--hdu1532(网络流 模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...

  3. 最短路——Dijkstra算法

    模板 水模板ing #include <cstdio> #include <cstring> #include <algorithm> #include <i ...

  4. 寒武纪camp Day2

    补题进度:8/10 A(计数+BIT) 题意: 给一个长度为n的数组a[],任意选0<=i<=j<n,将a[i]~a[j]从小到大排序,形成新的数组.问有多少个不同的新数组. N,a ...

  5. 禁用Bootstrap点击空白,modal自动关闭

    手动触发modal:       $('#myModal').modal(): 禁用点击空白,modal自动关闭:$('#myModal').modal({backdrop: 'static', ke ...

  6. cmd的操作命令导出导入.dmp文件

    利用cmd的操作命令导出,详情如下(备注:方法二是转载网上的教程): 1:G:\Oracle\product\10.1.0\Client_1\NETWORK\ADMIN目录下有个tnsname.ora ...

  7. System表空间大小有10Gb,使用率达到95%,

    System表空间大小有10Gb,使用率达到95%,很好奇, 随后执行如下SQL,查看system表空间中使用空间最多的对象 SQL>SELECT * FROM DBA_SEGMENTS T W ...

  8. datatable使用介绍

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 1.支持分页:前台分页和后台分页 前台分页:后台一次把数据传过来,交给前端渲染.缺点 ...

  9. 相机标定(Camera calibration)

    简单介绍 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程.也就是求终于的投影矩阵 P 的过程,以下相关的部分主要參考UIUC的计算机视觉的课件(网址Spr ...

  10. 数据结构与算法问题 AVL二叉平衡树

    AVL树是带有平衡条件的二叉查找树. 这个平衡条件必须保持,并且它必须保证树的深度是O(logN). 一棵AVL树是其每一个节点的左子树和右子树的高度最多差1的二叉查找树. (空树的高度定义为-1). ...