BZOJ 2154 Crash的数字表格 莫比乌斯反演

题目大意:求Σ[1<=i<=n]Σ[1<=j<=m]LCM(i,j) mod 20101009

题解见 http://www.cnblogs.com/jianglangcaijin/archive/2013/11/27/3446169.html

我到底写错什么了这么慢。。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 10001000
#define MOD 20101009
using namespace std;
typedef long long ll;
ll n,m,ans,sum[M];
int mu[M]={0,1},prime[1001001],tot;
bool not_prime[M];
void Linear_Shaker()
{
	int i,j;
	for(i=2;i<M;i++)
	{
		if(!not_prime[i])
			prime[++tot]=i,mu[i]=-1;
		for(j=1;j<=tot&&prime[j]*i<M;j++)
		{
			not_prime[prime[j]*i]=1;
			if(i%prime[j]==0)
			{
				mu[prime[j]*i]=0;
				break;
			}
			mu[prime[j]*i]=-mu[i];
		}
	}
	for(i=1;i<M;i++)
		sum[i]=(sum[i-1]+(long long)i*i*mu[i])%MOD;
}
inline ll Sum(ll x)
{
	return (x%MOD) * (x+1%MOD) %MOD * 10050505 %MOD;
}
ll F(int x,int y)
{
	int i,last;
	ll re=0;
	for(i=1;i<=x;i=last+1)
	{
		last=min(x/(x/i),y/(y/i));
		re+=Sum(x/i)*Sum(y/i)%MOD*(sum[last]-sum[i-1])%MOD;
		re%=MOD;
	}
	return re;
}
int main()
{
	int i,last;
	cin>>n>>m;
	Linear_Shaker();
	if(n>m) swap(n,m);
	for(i=1;i<=n;i=last+1)
	{
		last=min(n/(n/i),m/(m/i));
		ans+=F(n/i,m/i)*(Sum(last)-Sum(i-1))%MOD;
		ans%=MOD;
	}
	cout<<(ans+MOD)%MOD<<endl;
	return 0;
}


你可能感兴趣的:(bzoj,莫比乌斯反演,BZOJ2154)