51nod1244 莫比乌斯函数之和
推公式。f[n]=1-∑f[n/i](i=2...n)。然后递归+记忆化搜索。yyl说这叫杜教筛?时间复杂度貌似是O(n 2/3)的?
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ll long long
#define qwq(x) for(edge *o=head[x];o;o=o->next)
const int mod=1e6+7;
const int nmax=7e6+5;
struct edge{
int to,dis;edge *next;
};
edge es[mod],*pt=es,*head[nmax];
int mo[nmax+1],pe[nmax];bool vis[nmax+1];
void add(int u,int v,int d){
pt->to=v;pt->dis=d;pt->next=head[u];head[u]=pt++;
}
int get(ll n){
if(n<=nmax) return mo[n];
int tp=n%mod;
qwq(tp) if(o->to==n) return o->dis;
ll last,ans=0;
for(ll i=2;i<=n;i=last+1){
last=n/(n/i);
ans+=(last-i+1)*get(n/i);
}
add(tp,n,1-ans);
return 1-ans;
}
int main(){
mo[1]=1;int cnt=0,tp;
rep(i,2,nmax){
if(!vis[i]) pe[++cnt]=i,mo[i]=-1;
rep(j,1,cnt){
tp=pe[j];if(tp*i>nmax) break;vis[i*tp]=1;
if(i%tp==0){
mo[i*tp]=0;break;
} mo[i*tp]=-mo[i];
}
}
rep(i,1,nmax) mo[i]+=mo[i-1];
ll n,m;scanf("%lld%lld",&n,&m);
printf("%d\n",get(m)-get(n-1));
return 0;
}
输入包括两个数a, b,中间用空格分隔(2 <= a <= b <= 10^10)
输出S(a, b)。
3 10
-1
51nod1244 莫比乌斯函数之和的更多相关文章
- 51nod1244 莫比乌斯函数之和(杜教筛)
题面 传送门 题解 我--我忘记把预处理的块的大小调成\(n^{\frac{2}{3}}\)了--(仰天) 首先\(\mu*1=e\) 然后杜教筛就行了 //minamoto #include< ...
- 51nod1244 莫比乌斯函数之和 杜教筛
虽然都写了,过也过了,还是觉得杜教筛的复杂度好玄学 设f*g=h,∑f=S, 则∑h=∑f(i)S(n/i下取整) 把i=1时单独拿出来,得到 S(n)=(∑h-∑2->n f(i)S(n/i下 ...
- 51nod 1244 莫比乌斯函数之和
题目链接:51nod 1244 莫比乌斯函数之和 题解参考syh学长的博客:http://www.cnblogs.com/AOQNRMGYXLMV/p/4932537.html %%% 关于这一类求积 ...
- 51nod 1244 莫比乌斯函数之和 【杜教筛】
51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...
- [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)
[51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1Nμ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...
- 51 NOD 1244 莫比乌斯函数之和(杜教筛)
1244 莫比乌斯函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens) ...
- 51nod 1244 莫比乌斯函数之和(杜教筛)
[题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...
- 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...
- 莫比乌斯函数之和(51nod 1244)
莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如 ...
随机推荐
- const int *p与int *const p的区别(转:csdn,suer0101)
本文只是一篇学习笔记,是看了<彻底搞定C指针>中的相关篇幅后的一点总结,仅此而已! 一.先搞清const int *p与int const *p的区别 它们的区别就是:没有区别!! 无论谁 ...
- 一道有趣的javascript编程题
题目:实现以下功能 1. 点击按钮“打开新窗口”,打开新的子页面,要求新窗口的大小为400px X 200px 2. 输入地址信息,点击“确定”按钮,关闭该页面 3. 将子页面中输入的地址信息,回传到 ...
- super用法
Person类: public class Person { String _name; int _age; public Person(String name,int age) { _name= n ...
- iOS富文本-NSAttributedString简单封装
直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) { ; i < styles.count; i ++) { ...
- iOS第三方(显示视图的宽度高度)- MMPlaceHolder
github:https://github.com/adad184/MMPlaceHolder#readme appDelegate添加,影响全局 [MMPlaceHolderConfig defau ...
- 动态调用webservice 接口
1.url:http://localhost:8002/名称.asmx(asmx结尾) 2.需要引用的命名空间:System.Web.Services 3.调用代码: public class Dyn ...
- 全国DNS汇总
全国DNS汇总 来路不明的DNS服务器可能导致你的帐号密码轻易被盗,请谨慎使用!在中国大陆,最科学的方法是将首选DNS服务器设置为114.114.114.114,备用DNS服务器设置为当地电信运营商的 ...
- Linux网络编程9——对TCP与UDP的简易封装2.0
具体生成动态库的操作及使用该动态库的操作请参见上篇博文.以下仅仅列出改进版本的代码. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #define __MY_SOCKE ...
- node入门开发遇到的问题
最近在看node入门这本书,https://cnodejs.org/getstart 里面是跟随作者完成一个小的demo,书中不免会有遗漏的,下面是我在实现里面最后一个例子时遇到的问题,希望能够帮助其 ...
- 连接池和 "Timeout expired"异常【转】
异常信息: MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The tim ...