• 【洛谷P3327】约数个数和


    题目

    题目链接:https://www.luogu.com.cn/problem/P3327

    [sum^{n}_{i=1}sum^{m}_{j=1}d(ij) ]

    其中 (d(x)) 表示 (x) 的因子数量。多测。
    (Q,n,mleq 50000)

    思路

    可以证明 (d(ij)=sum_{x|i}sum_{y|j}[(x,y)=1])
    所以

    [sum^{n}_{i=1}sum^{m}_{j=1}d(ij) ]

    [=sum^{n}_{i=1}sum^{m}_{j=1}sum_{x|i}sum_{y|j}[(x,y)=1] ]

    枚举 (gcd(x,y)=d)

    [=sum^{n}_{i=1}sum^{m}_{j=1}sum_{d|i,d|j}sum_{x|frac{i}{d}}sum_{y|frac{j}{d}}mu(d) ]

    [=sum^{min(n,m)}_{d=1}mu(d)sum^{lfloorfrac{n}{d} floor}_{i=1}sum^{lfloorfrac{m}{d} floor}_{j=1}lfloorfrac{n}{id} floorlfloorfrac{m}{id} floor ]

    [=sum^{min(n,m)}_{d=1}mu(d)left ( sum^{lfloorfrac{n}{d} floor}_{i=1}lfloorfrac{lfloorfrac{n}{d} floor}{i} floor ight )left(sum^{lfloorfrac{m}{d} floor}_{j=1}lfloorfrac{lfloorfrac{m}{d} floor}{i} floor ight ) ]

    括号内的东西可以 (O(nsqrt{n})) 整除分块预处理,然后每次询问依然整出分块可以做到 (O(Qsqrt{n}))

    代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int N=50010;
    int Q,n,m,prm[N],mu[N];
    ll ans,f[N];
    bool v[N];
    
    void findprm(int n)
    {
    	mu[1]=1;
    	for (int i=2;i<=n;i++)
    	{
    		if (!v[i]) prm[++m]=i,mu[i]=-1;
    		for (int j=1;j<=m;j++)
    		{
    			if (i>n/prm[j]) break;
    			mu[i*prm[j]]=-mu[i]; v[i*prm[j]]=1;
    			if (!(i%prm[j])) { mu[i*prm[j]]=0; break; }
    		}
    	}
    }
    
    int main()
    {
    	findprm(N-1);
    	for (int i=1;i<N;i++) mu[i]+=mu[i-1];
    	for (int i=1;i<N;i++)
    		for (int l=1,r;l<=i;l=r+1)
    		{
    			r=i/(i/l);
    			f[i]+=1LL*(r-l+1)*(i/l);
    		}
    	scanf("%d",&Q);
    	while (Q--)
    	{
    		scanf("%d%d",&n,&m);
    		ans=0;
    		for (int l=1,r;l<=min(n,m);l=r+1)
    		{
    			r=min(n/(n/l),m/(m/l));
    			ans+=1LL*(mu[r]-mu[l-1])*f[n/l]*f[m/l];
    		}
    		printf("%lld
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    浅尝DesignPattern_AbstractFactory
    浅尝DesignPattern_OCP&DIP
    浅尝DesignPattern_Strategy
    浅尝EffectiveCSharp_2
    浅尝EffectiveCSharp_5
    浅尝EffectiveCSharp_3
    浅尝DesignPattern_Factory
    浅尝DesignPattern_Template
    我的ASP.NET之旅_基础知识&安装运行环境
    浅尝DesignPattern_Proxy
  • 原文地址:https://www.cnblogs.com/stoorz/p/14438333.html
Copyright © 2020-2023  润新知