zoukankan      html  css  js  c++  java
  • UVa11426 最大公约数之和(正版)

    题面

    (sum_{i=1}^{n-1}sum_{j=i+1}^{n}gcd(i, j))
    n<=4000000,数据组数T<=100
    答案保证在64位带符号整数范围内(long long就好)

    Sol

    之前做了一道假的
    先不管i,j是否有序,我们就求(sum_{i=1}^{n}sum_{j=1}^{n}gcd(i, j))
    最后(ans=(ans - (n + 1) * n / 2) / 2)即可
    推导
    (ans=sum_{d=1}^{n}sum_{i=1}^{lfloorfrac{n}{d} floor}mu(i)*lfloorfrac{n}{i*d} floor^2)
    (用k替换i*d,ans=sum_{k=1}^{n}lfloorfrac{n}{k} floor^2sum_{d|k}mu(frac{k}{d})d)
    (sum_{d|k}mu(frac{k}{d})d)是积性函数,线性筛即可
    加上数论分块

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Zsydalao 666
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(4e6 + 1);
     
    IL ll Read(){
        char c = '%'; ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        return x * z;
    }
     
    int prime[_], num;
    ll f[_];
    bool isprime[_];
     
    IL void Prepare(){
    	isprime[1] = 1; f[1] = 1;
    	for(RG int i = 2; i < _; ++i){
    		if(!isprime[i]) prime[++num] = i, f[i] = i - 1;
    		for(RG int j = 1; j <= num && i * prime[j] < _; ++j){
    			isprime[i * prime[j]] = 1;
    			if(i % prime[j])  f[i * prime[j]] = f[i] * f[prime[j]];
    			else{  f[i * prime[j]] = f[i] * prime[j]; break;  }
    		}
    	}
    	for(RG int i = 2; i < _; ++i) f[i] += f[i - 1];
    }
     
    int main(RG int argc, RG char *argv[]){
    	Prepare();
    	while(Zsydalao == 666){
    		RG ll n = Read(), ans = 0;
    		if(!n) break;
    		for(RG ll k = 1, j; k <= n; k = j + 1){
    			j = n / (n / k);
    			ans += (n / k) * (n / k) * (f[j] - f[k - 1]);
    		}
    		printf("%lld
    ", (ans - n * (n + 1) / 2) / 2);
    	}
    	return 0;
    }
    
  • 相关阅读:
    sqlilab less19-less22
    sqlilab less11-less18
    逆向——序列号相关总结
    xctf攻防世界——crackme writeup
    ESP定律脱壳——NsPack3.x脱壳
    避免全局变量漫天飞
    C语言实现队列
    STM32中的C语言知识点
    超时事件时间戳
    STM32-电源控制、低功耗模式
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8288009.html
Copyright © 2011-2022 走看看