雅礼集训 Function

第一道分块题


本文总阅读量
Posted by yjjr's blog on January 13, 2018

标签:分块,莫比乌斯反演,数论,积性函数

题目

这里写图片描述 这里写图片描述 这里写图片描述

分析

\[F(i)=\sum_{i=1}^n \sum_{d|i} \mu(d)\sigma_0^2 ( \frac i d )\] \[那么f(i)= \sum_{d|i} \mu(d)\sigma_0^2 ( \frac i d )\] \[考试的时候打表发现f(i)=\sigma_0 ( i^2 )\] \[那么F(i)=\sum_{i=1}^n f(i)=\sum_{i=1}^n\sigma_0 ( i^2 )\]

之后可以分块打表解决(F(i)和f(i)都是积性函数)

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
inline ll read()
{
	ll f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int maxn=1e6+6;
int n,prime[maxn],mu[maxn],d[maxn],s1[maxn],s2[maxn],cnt=0;
bool is_prime[maxn];
void getmu(){
	mu[1]=1;
	rep(i,2,maxn-6){
		if(!is_prime[i])prime[++cnt]=i,mu[i]=-1;
		rep(j,1,cnt){
			if(prime[j]*i>maxn-6)break;
			is_prime[prime[j]*i]=1;
			if(i%prime[j]==0){mu[i*prime[j]]=0;break;}else mu[i*prime[j]]=-mu[i];
		}
	}
}
ll querys1(int x){
	if(x<=maxn-6)return s1[x];
	int y=round(sqrt(x)+1);ll re=0;
	rep(i,1,y)re+=mu[i]*(x/(i*i));
	return re;
}
ll querys2(int x){
	if(x<=maxn-6)return s2[x];
	ll re=0;
	for(int i=1,j=1,k;i<=x;i=j+1){
		k=x/i,j=x/k;
		re+=(ll)(j-i+1)*k;
	}
	return re;
}
ll work(){
	ll re=0;
	for(int i=1,j=1,k;i<=n;i=j+1){
		k=n/i,j=n/k;
		re+=querys2(k)*(querys1(j)-querys1(i-1));
	}
	return re;
}
int main()
{
	//freopen("function.in","r",stdin);
	//freopen("function.out","w",stdout); 
	getmu();
	rep(i,1,maxn-6)
		rep(j,1,(maxn-6)/i)d[i*j]++;
	rep(i,1,maxn-6)s1[i]=s1[i-1]+mu[i]*mu[i],s2[i]=s2[i-1]+d[i];
	int T=read();
	if(T==0){cout<<endl;return 0;}
	while(T--){
		n=read();
	    cout<<work()<<endl;
	}
	return 0;
}

本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr