• 51nod1227 平均最小公倍数


    [egin{aligned} ans&=sum_{n=1}^{b}frac{1}{n}sum_{i=1}^{n} ext{lcm}(i,n)\ &=sum_{n=1}^{b}frac{1}{n}sum_{i=1}^{n}frac{in}{gcd(i,n)}\ &=sum_{n=1}^{b}sum_{d|n}frac{1}{d}sum_{i=1}^{n}i[gcd(i,n)=d]\ &=sum_{n=1}^{b}sum_{dmid n}sum_{i=1}^{n/d}i[gcd(i,n/d)=1]\ &=sum_{n=1}^{b}sum_{dmid n}frac{varphi(frac{n}{d}) imesfrac{n}{d}}{2}\ &=sum_{n=1}^{b}sum_{dmid n}frac{varphi(d) imes d}{2}\ &=sum_{d=1}^{b}frac{varphi(d) imes d}{2}lfloorfrac{n}{d} floor end{aligned} ]

    [sum_{i=1}^{n}i[gcd(i,n)=1]=egin{cases} frac{varphi(n) imes n}{2}(n>1)\ 1(n=1) end{cases} ]

    差分一下即可。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll inf=0x3f3f3f3f3f3f3f3f;
    const int MAXN=300005;
    const ll mod=1e9+7;
    const ll inv2=mod-mod/2;
    const ll inv6=166666668;
    template <typename T>
    void read(T &x) {
    	T flag=1;
    	char ch=getchar();
    	for (; '0'>ch||ch>'9'; ch=getchar()) if (ch=='-') flag=-1;
    	for (x=0; '0'<=ch&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
    	x*=flag;
    }
    ll a, b;
    map<ll, ll> mp;
    ll sum[1000005], prime[1000005], phi[1000005];
    int cnt;
    bool mark[1000005];
    void sieve() {
    	for (int i=2; i<=1000000; i++) {
    		if (!mark[i]) {
    			prime[++cnt]=i;
    			phi[i]=i-1;
    		}
    		for (int j=1; j<=cnt&&prime[j]*i<=1000000; j++) {
    			mark[prime[j]*i]=true;
    			if (i%prime[j]==0) {
    				phi[i*prime[j]]=phi[i]*prime[j];
    			} else {
    				phi[i*prime[j]]=phi[i]*(prime[j]-1);
    			}
    		}
    	}
    	sum[1]=1;
    	for (int i=2; i<=1000000; i++) {
    		sum[i]=(sum[i-1]+phi[i]*i%mod)%mod;
    	}
    }
    ll calc(ll i, ll j) {
    	return (i+j)*(j-i+1)%mod*inv2%mod;
    }
    ll get_sum(ll n) {
    	if (n<=1000000) return sum[n];
    	if (mp.find(n)!=mp.end()) return mp[n];
    	ll ret=n*(n+1)%mod*(2*n+1)%mod*inv6%mod;
    	for (ll i=2, j=0; i<=n; i=j+1) {
    		j=n/(n/i);
    		ret=(ret-calc(i, j)%mod*get_sum(n/i)%mod+mod)%mod;
    	}
    	return mp[n]=ret;
    }
    ll solve(ll n) {
    	if (n<=0) return 0;
    	ll ret=0;
    	for (ll i=1, j=0; i<=n; i=j+1) {
    		j=n/(n/i);
    		ret=(ret+(get_sum(j)-get_sum(i-1)+mod)%mod*(n/i)%mod)%mod;
    	}
    	return (ret+n)*inv2%mod;
    }
    int main() {
    	sieve();
    	read(a); read(b);
    	printf("%lld
    ", (solve(b)-solve(a-1)+mod)%mod);
    	return 0;
    }
    
  • 相关阅读:
    CentOS 6.3下安装腾达USB无线网卡遇到的问题及解决方法
    验证resneXt,densenet,mobilenet和SENet的特色结构
    比较语义分割的几种结构:FCN,UNET,SegNet,PSPNet和Deeplab
    比较 VGG, resnet和inception的图像分类效果
    强化学习基础
    深度学习的知识点
    卷积神经网络CNN
    信息论的知识点
    理论机器学习
    计算理论基础
  • 原文地址:https://www.cnblogs.com/zcr-blog/p/14438120.html
Copyright © 2020-2023  润新知