• P1447 [NOI2010]能量采集


    gate

    一个植物 ((x,y))((0,0))上的连线共有 (gcd(x,y))个点,再减去两个端点,
    能量损失即为(2 imes(gcd(x,y)-2)+1 = 2 imes gcd(x,y)+1)

    所以题目要求的即:

    (sumlimits_{i=1}^nsumlimits_{j=1}^m(2 imes gcd(i,j)-1))

    (=2 imessumlimits_{i=1}^nsumlimits_{j=1}^mgcd(i,j) - n imes m)

    (f=sumlimits_{i=1}^nsumlimits_{j=1}^mgcd(i,j))

    (=sumlimits_{d=1}^ndsumlimits_{i=1}^nsumlimits_{j=1}^m[gcd(i,j)=d])

    (=sumlimits_{d=1}^ndsumlimits_{i=1}^{frac{n}{d}}sumlimits_{j=1}^frac{m}{d}sumlimits_{k|i,k|j}mu(k))

    (=sumlimits_{d=1}^ndsumlimits_{k=1}^frac{n}{d}mu(k)dfrac{n}{kd} imesdfrac{m}{kd})

    (T = kd)

    (=sumlimits_{T=1}^ndfrac{n}{T} imesdfrac{m}{T}sumlimits_{k|T}mu(k) imesdfrac{T}{k})

    根据(id imesmu=varphi)

    (=sumlimits_{T=1}^ndfrac{n}{T} imesdfrac{m}{T} imesvarphi(T))

    原式即(=2 imessumlimits_{T=1}^nlfloordfrac{n}{T} floor imeslfloordfrac{m}{T} floor imesvarphi(T) - n imes m)

    时间复杂度(O(n))

    code

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #define MogeKo qwq
    using namespace std;
    
    const int maxn = 1e5+10;
    const int N = 1e5;
    
    int phi[maxn],prime[maxn],cnt;
    long long n,m,sum[maxn];
    bool vis[maxn];
    
    void Phi() {
    	sum[1] = phi[1] = 1;
    	for(int i = 2; i <= N; i++) {
    		if(!vis[i]) {
    			phi[i] = i-1;
    			prime[++cnt] = i;
    		}
    		for(int j = 1; j <= cnt && i*prime[j] <= N; j++) {
    			vis[i*prime[j]] = true;
    			if(i % prime[j])
    				phi[i*prime[j]] = phi[i] * (prime[j]-1);
    			else {
    				phi[i*prime[j]] = phi[i] * prime[j];
    				break;
    			}
    		}
    		sum[i] = sum[i-1] + phi[i];
    	}
    }
    
    long long f(long long n,long long m) {
    	long long ans = 0;
    	if(n > m) swap(n,m);
    	for(int i = 1,r; i <= n; i = r+1) {
    		r = min(n/(n/i),m/(m/i));
    		ans += (n/i)*(m/i)*(sum[r]-sum[i-1]);
    	}
    	return 2 * ans - n*m;
    }
    
    int main() {
    	scanf("%lld%lld",&n,&m);
    	Phi();
    	printf("%lld",f(n,m));
    	return 0;
    }
    
  • 相关阅读:
    java类研究(String)
    webservices
    LoadRunner(软件性能测试工具)
    java线程
    lucene solr
    java IO
    实现一个可变长数组
    [北大程序设计与算法]--虚函数与多态的实例
    A1155 Heap Paths [堆的dfs]
    A1154 Vertex Coloring
  • 原文地址:https://www.cnblogs.com/mogeko/p/13343933.html
Copyright © 2020-2023  润新知