BZOJ2154 Crash的数字表格

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 

本文作者:ljh2000 
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

Description

今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple)。对于两个正整数a和b,LCM(a, b)表示能同时被a和b整除的最小正整数。例如,LCM(6, 8) = 24。回到家后,Crash还在想着课上学的东西,为了研究最小公倍数,他画了一张N*M的表格。每个格子里写了一个数字,其中第i行第j列的那个格子里写着数为LCM(i, j)。一个4*5的表格如下: 1 2 3 4 5 2 2 6 4 10 3 6 3 12 15 4 4 12 4 20 看着这个表格,Crash想到了很多可以思考的问题。不过他最想解决的问题却是一个十分简单的问题:这个表格中所有数的和是多少。当N和M很大时,Crash就束手无策了,因此他找到了聪明的你用程序帮他解决这个问题。由于最终结果可能会很大,Crash只想知道表格里所有数的和mod 20101009的值。

Input

输入的第一行包含两个正整数,分别表示N和M。

Output

输出一个正整数,表示表格中所有数的和mod 20101009的值。

Sample Input

4 5

Sample Output

122
【数据规模和约定】
100%的数据满足N, M ≤ 10^7。

 

正解:线性筛+莫比乌斯反演

解题报告:

  我跟网上的推导方法都不太一样,似乎我的更好理解一下吧...

  下面是我的推导过程:(不妨设n<m)

  ${\sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)}$

  ${=\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{gcd(i,j)}}$

  ${=\sum_{g=1}^{n}\sum_{i=1}^{\left \lfloor \frac{n}{g} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{g} \right \rfloor}\sum_{t|i,t|j}\mu (t)ijg}$

  令$S[n]=\sum_{i=1}^{n}i$,则

  原式${=\sum_{g=1}^{n}g\sum_{t=1}^{\left\lfloor\frac{n}{g}\right\rfloor}\mu (t)t^2 S(\left \lfloor \frac{n}{gt} \right \rfloor) S(\left \lfloor \frac{m}{gt} \right \rfloor)}$

  令$Q=gt$,换元得:

  原式${=\sum_{Q=1}^{n}S(\left \lfloor \frac{n}{Q} \right \rfloor)S(\left \lfloor \frac{m}{Q} \right \rfloor)Q\sum_{t|Q}\mu (t)t}$

   不妨设${f(n)=\sum_{t|n}\mu(t)t}$

   则$f(n)$为积性函数,可以在$O(n)$的复杂度内,做线性筛时顺便递推得到:

     ${f(i*p)=f(i)*f(p) ,}$$i$ $mod$ $p$ ${!=0}$

      ${=f(i) ,}$$i$ $mod$ $p$ ${=0}$

  (p为质数)

  总复杂度:$O(n)$

 

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 10000011;
const int MOD = 20101009;
int n,m,cnt,ans;
int prime[MAXN],S[MAXN],f[MAXN];
bool vis[MAXN];

inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void work(){
	n=getint(); m=getint(); if(n>m) swap(n,m); LL now;
	f[1]=1;
	for(int i=2;i<=n;i++) {
		if(!vis[i]) { prime[++cnt]=i; f[i]=-i+1; }
		for(int j=1;j<=cnt && i*prime[j]<=n;j++) {
			vis[i*prime[j]]=1;
			if(i%prime[j]==0) { f[i*prime[j]]=f[i]; break; }
			now=(LL)f[i]*f[prime[j]]; now%=MOD;
			f[i*prime[j]]=now;
		}
	}
	for(int i=1;i<=m;i++) S[i]=S[i-1]+i,S[i]%=MOD;
	for(int Q=1;Q<=n;Q++) {
		now=(LL)S[n/Q]*S[m/Q]; now%=MOD; 
		now*=f[Q]; now%=MOD;
		now*=Q; now%=MOD;
		ans+=now; ans%=MOD;
	}
	ans+=MOD; ans%=MOD;
	printf("%d",ans);
}
 
int main()
{
    work();
    return 0;
}

  

posted @ 2017-01-30 14:41  ljh_2000  阅读(1629)  评论(2编辑  收藏  举报