【bzoj2154】【Crash的数字表格】【莫比乌斯反演】

来源:互联网 发布:家庭网限制80端口吗 编辑:程序博客网 时间:2024/06/04 19:00

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≤ 107.
题解:
同bzoj2693,还是单组询问。

#include<iostream>#include<cstdio>#include<cstring>#define N 10000010#define P 20101009#define LL long longusing namespace std;int p[N],f[N],n,m,T,pos;LL g[N],ans,s[N];void pre(){  g[1]=1;  for (int i=2;i<=n;i++){    if (!f[i]){p[++p[0]]=i;g[i]=(1-i+P)%P;}    for (int j=1;j<=p[0]&&i*p[j]<=n;j++){      f[i*p[j]]=1;      if (i%p[j]==0){g[i*p[j]]=g[i];break;}      g[i*p[j]]=(g[i]*g[p[j]])%P;    }  }  for (int i=1;i<=n;i++) g[i]=(g[i]*i)%P;  for (int i=1;i<=n;i++) s[i]=(s[i-1]+g[i])%P;}LL sum(LL x,LL y){  LL t1=x*(x+1)/2;LL t2=y*(y+1)/2;  t1%=P;t2%=P;  return (t1*t2)%P; }int main(){     scanf("%d%d",&n,&m);pre();     if (n>m) swap(n,m);ans=0;pos=0;     for (int i=1;i<=n;i=pos+1){       pos=min((n/(n/i)),(m/(m/i)));       ans+=(sum((LL)(n/i),(LL)(m/i))*(s[pos]-s[i-1]+P)%P)%P;       ans%=P;     }       printf("%lld",ans);    } 
0 0