51nod 1244 莫比乌斯函数之和
题解参考syh学长的博客:http://www.cnblogs.com/AOQNRMGYXLMV/p/4932537.html %%%
关于这一类求积性函数前缀和的方法,学习参考博客:http://blog.csdn.net/skywalkert/article/details/50500009
要好好看大神的博客哦orz
用筛法预处理前N^(2/3)项,后面的记忆化搜索解决。
不太会用哈希,就用map记忆化一下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define CLR(a,b) memset((a),(b),sizeof((a)))
typedef long long ll;
const int N = ;
bool check[N+];
int prime[N+];
int mu[N+];
ll mu_sum[N+];
void Moblus(){
CLR(check, false);
mu[] = ;
int tot = ;
for(int i = ; i <= N; i++){
if( !check[i] ){
prime[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot && i*prime[j] <= N; j++){
check[i * prime[j]] = true;
if( i % prime[j] == ){
mu[i * prime[j]] = ;
break;
}
else
mu[i * prime[j]] = -mu[i];
}
}
for(int i = ; i <= N; ++i)
mu_sum[i] = mu_sum[i-] + mu[i];
}
map<ll,ll> mp;
ll Mertens(ll n){
if(n <= N) return mu_sum[n];
if(mp.count(n)) return mp[n];
ll ans = , ed;
for(ll i = ; i <= n; i = ed+){
ed = n/(n/i);
ans -= (ed - i + )*Mertens(n/i);
}
return mp[n] = ans;
}
int main(){
Moblus();
ll a, b;
scanf("%lld%lld", &a, &b);
printf("%lld\n", Mertens(b) - Mertens(a-));
return ;
}
51nod 1244 莫比乌斯函数之和的更多相关文章
- 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== ...
- 51nod 1244 莫比乌斯函数之和(杜教筛)
[题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...
- 51Nod.1244.莫比乌斯函数之和(杜教筛)
题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...
- 51nod 1244 莫比乌斯函数之和 【莫比乌斯函数+杜教筛】
和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) \ ...
- 51 NOD 1244 莫比乌斯函数之和(杜教筛)
1244 莫比乌斯函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens) ...
- 【51nod】1244 莫比乌斯函数之和
题解 求积性函数的前缀和?杜教筛! 这不给一发杜教筛入门必备之博客= = https://blog.csdn.net/skywalkert/article/details/50500009 好了,然后 ...
- 51nod1244 莫比乌斯函数之和
推公式.f[n]=1-∑f[n/i](i=2...n).然后递归+记忆化搜索.yyl说这叫杜教筛?时间复杂度貌似是O(n 2/3)的? #include<cstdio> #include& ...
- 51nod 1240 莫比乌斯函数
题目链接:51nod 1240 莫比乌斯函数 莫比乌斯函数学习参考博客:http://www.cnblogs.com/Milkor/p/4464515.html #include<cstdio& ...
随机推荐
- HTML 字符图案
Dog: <!-- :: :;J7, :, ::;7: ,ivYi, , ;LLLFS: :iv7Yi :7ri;j5PL ,:ivYLvr ,ivrrirrY2X, :;r@Wwz.7r: : ...
- MessageReceiver
class MessageReceiver { private RelayEngine<MessageCollection> _MessageRelayEngine; private st ...
- PHP之初识PHP(1)
酸菜记 之 初识PHP 这是我从零基础了解PHP开始学习的笔记,共同学习,有不对之处,望指出共同学习. 一.初了解PHP PHP 是超文本预处理语言 php HyperText Preprocesso ...
- 百度之星初赛2A 1001 ALL X(HDU 5690)
All X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- tensorflow安装日志(PIP)
最近刚刚接触深度学习,安装一下tf 环境:华硕Z170主板.i7 6700k.GTX1070.Ubuntu16.04.Python2.7 在这之前先装好了cuda8.0.cudnn5.0.caffe整 ...
- js 操作数组封装
function OperateArray(array) { this.array = array; } OperateArray.prototype.hasValue = function(val) ...
- HDU1760 A New Tetris Game NP态
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 内置函数callable(object)
如果对象object参数是可以调用的对象,就返回True:否则返回False.不过要注意的是,当一个对象是可以调用的,并不表示调用该对象时执行一定成功,但不可调用的对象去调用时一定不会成功.如果类对象 ...
- 如何修改Hadoop的默认日志级别,还真是麻烦
鄙人使用的Hadoop版本为2.6.4.Hadoop的默认日志级别为INFO,对于百台以上的集群,如果文件操作频繁的话,NameNode会狂打日志,对性能会有一定的影响. 我们可以通过http://& ...
- consul 模板配置内容的参数说明
datacenters {{datacenters}} 数据中心 file {{file "/path/to/local/file"}} 读取本地文件的内容.如果不可读的话,会报错 ...