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.例如 ...
随机推荐
- 帝国cms无法注册登录快速解决方法 附路径
帝国cms安装相对比较简单,一路next,一般从网上下载的系统都会带一些数据,恢复备份数据后,清除缓存,更新数据,一个copy版的网站就出来了.但是为了se的友好需要改动很多地方,不然很容易被认为是s ...
- 解决iptables和vsftpd设置的问题
解决iptables和vsftpd设置的问题 博客分类: linux/centos/ubuntu 防火墙J#工作 解决iptables和vsftpd设置的问题 修改 vi /etc/sysconfig ...
- POJ 3180
The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1132 Accepted: 713 Descr ...
- lightoj 1397 - Sudoku Solver
思路:每次找出可能情况最少的位置枚举可能情况!!! poj2676和这题一样不过poj数据比较水,很容易过. 代码如下: #include<iostream> #include<cs ...
- junit中的assert方法总结
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- CS001: 清理浮动的几种方法以及对应规范说明
以下内容转自 http://www.w3help.org/zh-cn/casestudies/001 <!==================> 前言 浮动 Floats 是 CSS 中的 ...
- zend studio10破解
http://blog.csdn.net/qq1355541448/article/details/16807429
- jquery easyUi 配置默认页码
jquery easyUI用pagenation 属性如果修改其默认加载页面显示,配置该怎样写? 注意区分datagrid的pagenation分页的区别,代码如下. if ($.fn.paginat ...
- CentOS安装Chrome
问题 在CentOS安装Chrome会遇到 libstdc++.so.6(GLIBCXX_3.4.15)(64bit) 依赖失败的问题, 即使下载了最新的libstdc++.so.6(包含GLIBCX ...
- java web多线程
1.多线程并发时,多个线程同时请求同一个资源,必然导致此资源的数据不安全,A线程修改了B线 程的处理的数据,而B线程又修改了A线程处理的数理.显然这是由于全局资源造成的,有时为了解 决此问题,优先考虑 ...