关于积性函数前缀和的问题,可以关注糖老师的博客
http://blog.csdn.net/skywalkert/article/details/50500009
推导不写了
结论是M(n)=∑ni=1u(i)
M(n)=1−∑ni=2M(ni)
直接递归求解,对于n,直接sqrt(n)的复杂度枚举i,然后递归求解
蓝儿还是会T,用map记忆化一下,小于500W的前缀和可以直接预处理,能过
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 5000005
#define MAXN 1000005
#define maxnode 105
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool prime[MAX];
int pr[MAX];
int tot;
int mu[MAX];
map<LL,LL> ma;
void init(){
mem(prime,0);tot=0;
mu[0]=0;mu[1]=1;
for(int i=2;i<MAX;i++){
if(!prime[i]){
pr[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot&&pr[j]*i<MAX;j++){
prime[pr[j]*i]=1;
if(i%pr[j]==0){
mu[i*pr[j]]=0;
break;
}
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<MAX;i++) mu[i]+=mu[i-1];
}
LL cal(LL a){
if(a<MAX) return mu[a];
if(ma.count(a)) return ma[a];
LL ans=1;
for(LL i=2;i<=a;){
LL x=a/(a/i);
ans-=cal(a/i)*(x-i+1);
i=x+1;
}
ma[a]=ans;
return ans;
}
int main(){
//freopen("in.txt","r",stdin);
LL a,b;
init();
ma.clear();
cin>>a>>b;
cout<<cal(b)-cal(a-1)<<endl;
return 0;
}