zoukankan      html  css  js  c++  java
  • luoguP2257 YY的GCD

    题意

    (f(d)=sumlimits_{i=1}^{n}sumlimits_{j=1}^{m}[gcd(i,j)=d],F(d)=sumlimits_{k|d}f(k)=frac{n}{d}*frac{m}{d})

    得:
    (f(n)=sumlimits_{n|d}mu(frac{d}{n})F(d))

    (ans=sumlimits_{pin prime}f(p))
    (=sumlimits_{pin prime}sumlimits_{p|d}mu(frac{d}{p})*F(d))
    (frac{d}{p}=t)
    (=sumlimits_{pin prime}sumlimits_{t=1}^{min(frac{n}{p},frac{m}{p})}mu(t)*F(t*p))
    (=sumlimits_{pin prime}sumlimits_{t=1}^{min(frac{n}{p},frac{m}{p})}mu(t)*frac{n}{t*p}*frac{m}{t*p})
    (t*p=T)
    (=sumlimits_{T=1}^{min(n,m)}sumlimits_{t|T,tin prime}mu(frac{T}{t})*frac{n}{T}*frac{m}{T})
    (=sumlimits_{T=1}^{min(n,m)}frac{n}{T}*frac{m}{T}*(sumlimits_{t|T,tin prime}mu(frac{T}{t})))

    于是就可以做了。

    code:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=10000010;
    int T,n,m;
    int mu[maxn],sum[maxn];
    bool vis[maxn];
    vector<int>prime;
    inline void shai(int n)
    {
    	vis[1]=1;mu[1]=1;
    	for(int i=2;i<=n;i++)
    	{
    		if(!vis[i])prime.push_back(i),mu[i]=-1;
    		for(unsigned int j=0;j<prime.size()&&i*prime[j]<=n;j++)
    		{
    			vis[i*prime[j]]=1;
    			if(i%prime[j]==0){mu[i*prime[j]]=0;break;}
    			mu[i*prime[j]]=-mu[i];
    		}
    	}
    	for(unsigned int i=0;i<prime.size();i++)
    		for(int j=1;j*prime[i]<=n;j++)
    			sum[j*prime[i]]+=mu[j];
    	for(int i=1;i<=n;i++)sum[i]+=sum[i-1];
    }
    inline ll solve(int n,int m)
    {
    	ll res=0;
    	for(int l=1,r;l<=min(n,m);l=r+1)
    	{
    		r=min(n/(n/l),m/(m/l));
    		res+=1ll*(n/l)*(m/l)*(sum[r]-sum[l-1]);
    	}
    	return res;
    }
    int main()
    {
    	shai(10000000);
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d%d",&n,&m);
    		printf("%lld
    ",solve(n,m));
    	}
    	return 0;
    }
    
  • 相关阅读:
    弹性盒模型
    CSS3属性
    CSS3选择器
    闭包
    angularjs-select2的使用
    angular 分页插件的使用
    webstorm 破解
    数组和字符串之间的转化
    git 拉取分支代码 合分支
    时间戳转化为时间格式 时间格式转为时间戳
  • 原文地址:https://www.cnblogs.com/nofind/p/11939840.html
Copyright © 2011-2022 走看看