BZOJ 2154 Crash的数字表格

来源:互联网 发布:美发店会员软件 编辑:程序博客网 时间:2024/05/06 04:11

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。

HINT

Source

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

莫比乌斯反演~

debug到怀疑人生,然后发现是读入优化写错了……

思路见学姐的博客:http://blog.csdn.net/regina8023/article/details/44243911~

(取模加括号,过程开long long,不然WA到飞起= =)


#include<cstdio>#include<iostream>using namespace std;#define modd 20101009#define N 10000001#define ll long longint n,m,miu[N],c[1000001],num[N];ll ans,ne;bool bb[N];int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}ll nn(ll u,ll v){return (((u*(u+1)/2)%modd)*((v*(v+1)/2)%modd))%modd;}ll cal(ll a,ll b,ll k){a/=k;b/=k;if(a>b) swap(a,b);if(!a) return 0;ll tot=0,ne;for(ll i=1;i<=a;i=ne){ne=min(a/(a/i),b/(b/i))+1;tot=(tot+((num[ne-1]-num[i-1])*nn(a/i,b/i)%modd))%modd;}return tot;}int main(){n=read();m=read();if(n>m) swap(n,m);miu[1]=1;for(int i=2;i<=n;i++){if(!bb[i]) c[++c[0]]=i,miu[i]=-1;for(int j=1;j<=c[0] && c[j]*i<=n;j++){bb[i*c[j]]=1;if(i%c[j]) miu[i*c[j]]=-miu[i];else break;}}for(ll i=1;i<=n;i++) num[i]=(num[i-1]+(i*i*miu[i])%modd)%modd;for(ll i=1;i<=n;i=ne){ne=min(n/(n/i),m/(m/i))+1;ans=(ans+((i+ne-1)*(ne-i)/2)%modd*cal(n,m,i)%modd)%modd;}printf("%lld\n",(ans+modd)%modd);return 0;}


1 0
原创粉丝点击