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

P1829 [国家集训队]Crash的数字表格 / JZPTAB

时间:2019-01-05 15:09:40      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:ret   while   scanf   getch   枚举   print   return   def   lse   

P1829 [国家集训队]Crash的数字表格 / JZPTAB

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

思考把\(\sum_{d|n}\mu(d)=[n=1]\)带进去

\(Ans=\sum_{d=1}^{min(n,m)}\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)=d]\frac{ij}{d}\)

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i,j)=1]ij\)

至此,我们把式变成:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum_{x|gcd(i,j)}\mu(x)ij\)

我们来枚举\(x\)消掉\(\sum_{x|gcd(i,j)}\)

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(x)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}ij[x|gcd(i,j)]\)

对于判断\([x|gcd(i,j)]\)也要消掉:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(x)\sum_{xu=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{xv=1}^{\lfloor\frac{m}{d}\rfloor}x^2uv\)

最后要处理的式子:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}x^2\mu(x)(\sum_{u=1}^{\lfloor\frac{n}{dx}\rfloor}u)(\sum_{v=1}^{\lfloor\frac{m}{dx}\rfloor}v)\)

直接分块,另\((\sum_{u=1}^{\lfloor\frac{n}{dx}\rfloor}u)(\sum_{v=1}^{\lfloor\frac{m}{dx}\rfloor}v)\)相同

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL p=20101009;
const int maxn=1e7+10;
inline int Read(){
    LL x=0,f=1; char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1; c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-'0',c=getchar();
    }
    return x*f;
}
int n,m;
LL ans;
int mu[maxn],prime[maxn];
LL sum[maxn];
bool visit[maxn];
inline void F_phi(LL max_n){
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=max_n;++i){
        if(!visit[i]){
            prime[++tot]=i,
            mu[i]=-1;
        }
        for(int j=1;j<=tot&&i*prime[j]<=max_n;++j){
            visit[i*prime[j]]=true;
            if(i%prime[j]==0)
                break;
            else
                mu[i*prime[j]]=-mu[i];
        }
    }
    for(int i=1;i<=max_n;++i)
        sum[i]=(sum[i-1]+(LL)mu[i]*i%p*i%p)%p;
}
int main(){
    scanf("%d%d",&n,&m);
    int N=min(n,m);
    F_phi(N);
    for(int d=1;d<=N;++d){
        int maxx=n/d,maxy=m/d;
        int x=min(maxx,maxy);
        LL num=0;
        for(int l=1,r;l<=x;l=r+1){
            r=min(maxx/(maxx/l),maxy/(maxy/l));
            num=(num+
            ((sum[r]-sum[l-1]+p)%p)*
            ((((1ll+maxx/l)%p)*1ll*(maxx/l)/2%p)%p)%p*
            ((((1ll+maxy/l)%p)*1ll*(maxy/l)/2%p)%p)%p)%p;
        }
        ans=(ans+(num*1ll*d)%p)%p;
    }
    printf("%lld",ans);
    return 0;
}/*
1000000 1000000
9002207
*/

P1829 [国家集训队]Crash的数字表格 / JZPTAB

标签:ret   while   scanf   getch   枚举   print   return   def   lse   

原文地址:https://www.cnblogs.com/y2823774827y/p/10223338.html

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