码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ 2154】Crash的数字表格

时间:2015-08-04 21:02:09      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

我们定义sum(n,m)=ni=1mj=1i?j
ni=1mj=1lcm(i,j)=ni=1mj=1i?jgcd(i,j)
=?nd?i=1?md?j=1di?djde(gcd(i,j))
=min(n,m)d=1d?nd?i=1?md?j=1i?je(gcd(i,j))
=min(n,m)d=1dmin(?nd?,?md?)k=1μ(k)?k2?sum(??nd?k?,??md?k?)
由于?nd???md?k?各有n?nd?个取值,因而我们可以分块统计,code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define P 20101009LL
using namespace std;
long long n,m,tot=0,mu[10000001],pri[700001];
long long pre[10000001],sum[10000001];
bool f[10000001];
long long get(long long n,long long m)
{
    long long i,last=0; long long ans=0;
    if (n>m) swap(n,m);
    for (i=1;i<=n;i=last+1)
      {
        last=min(n/(n/i),m/(m/i));
        ans=(ans+(long long)(((long long)((sum[last]-sum[i-1]+P)%P)*(long long)(((long long)((((m/i)*(m/i+1))/2)%P)*(long long)((((n/i)*(n/i+1))/2)%P))%P)))%P)%P;
        if (ans<0) cout<<‘!‘<<endl;
      }
    return ans;
}
long long work(long long n,long long m)
{
    long long i,last=0; long long ans=0;
    if (n>m) swap(n,m);
    for (i=1;i<=n;i=last+1)
      {
        last=min(n/(n/i),m/(m/i));
        ans=(ans+(long long)((get(n/i,m/i)%P*(long long)((pre[last]-pre[i-1]+P)%P)))%P)%P;
        if (ans<0) cout<<‘!‘<<endl;
      }
    return ans;
}
int main()
{
    long long i,j,maxn;
    scanf("%I64d%I64d",&n,&m);
    mu[1]=1;maxn=max(n,m);
    for (i=2;i<=maxn;++i)
      {
        if (!f[i])
          {
            mu[i]=-1;
            pri[++tot]=i;
          }
        for (j=1;j<=tot&&i*pri[j]<=maxn;++j)
          {
            f[i*pri[j]]=true;
            if (i%pri[j]==0)
              {
                mu[i*pri[j]]=0;
                break;
              }
            else mu[i*pri[j]]=-mu[i];
          }
      }
    for (i=1;i<=maxn;++i) {sum[i]=(sum[i-1]+(long long)((i*i)%P*mu[i])%P+P)%P; pre[i]=(long long)(pre[i-1]+i)%P;}
    printf("%I64d\n",work(n,m));
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

【BZOJ 2154】Crash的数字表格

标签:

原文地址:http://blog.csdn.net/lcomyn/article/details/47282061

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!