zoukankan      html  css  js  c++  java
  • 【51Nod 1222】最小公倍数计数

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222
    ([a,b])中的个数转化为求([1,b])中的个数减去([1,a))中的个数。

    [egin{aligned} &sum_{i=1}^nsum_{j=1}^nleft[frac{ij}{(i,j)}leq n ight]\ =&sum_{d=1}^nsum_{i=1}^{leftlfloorfrac nd ight floor}sum_{j=1}^{leftlfloorfrac nd ight floor}[(i,j)=1][ijdleq n]\ =&sum_{d=1}^nsum_{i=1}^{leftlfloorfrac nd ight floor}sum_{j=1}^{leftlfloorfrac nd ight floor}sum_{d'|(i,j)}mu(d')[ijdleq n]\ =&sum_{d=1}^nsum_{d'=1}^{leftlfloorfrac nd ight floor}mu(d')sum_{i=1}^{leftlfloorfrac n{dd'} ight floor}sum_{j=1}^{leftlfloorfrac n{dd'} ight floor}left[ijdd'^2leq n ight]\ =&sum_{d'=1}^{leftlfloorsqrt n ight floor}mu(d')sum_{d=1}^{leftlfloorfrac n{d'} ight floor}sum_{i=1}^{leftlfloorfrac n{dd'} ight floor}sum_{j=1}^{leftlfloorfrac n{dd'} ight floor}left[ijdleqleftlfloorfrac n{d'^2} ight floor ight]\ end{aligned} ]

    枚举(leftlfloorsqrt n ight floor)(d'),对于每个(d'),先假定(dleq ileq j),枚举(leftlfloorleft(frac n{d'} ight)^{frac 13} ight floor)(d)(i)再从(d)枚举到(leftlfloorleft(frac n{dd'} ight)^{frac 12} ight floor),最后计算(j)的个数,乘上一个排列数即可。
    (f(x))表示对每个(d')计算的复杂度。
    时间复杂度为(Oleft(sumlimits_{d'=1}^{leftlfloorsqrt n ight floor}f(d') ight))

    [egin{aligned} f(x)=&int_{0}^{left(frac n{x^2} ight)^{frac 13}}left(left(frac n{x^2i} ight)^{frac 12}-i ight)di\ =&left(frac n{x^2} ight)^{frac 12}int_0^{left(frac n{x^2} ight)^{frac 13}}i^{-frac 12}di-int_0^{left(frac n{x^2} ight)^{frac 13}}idi\ =&left(frac n{x^2} ight)^{frac 12}left(2left({left(frac n{x^2} ight)^{frac 13}} ight)^{frac 12}-2 ight)-left(frac 12left({left(frac n{x^2} ight)^{frac 13}} ight)^2 ight)\ =&frac 34left(frac n{x^2} ight)^{frac 23}-2left(frac n{x^2} ight)^{frac 12} end{aligned} ]

    [egin{aligned} &Oleft(sum_{d'=1}^{leftlfloorsqrt n ight floor}f(d') ight)\ =&Oleft(int_0^{sqrt n}left(frac n{x^2} ight)^{frac 23}dx ight)\ =&Oleft(n^{frac 23}int_0^{sqrt n}x^{-frac 43}dx ight)\ =&Oleft(n^{frac 23}left(left(-3sqrt{n}^{-frac 13} ight)-left(-3 imes0^{-frac 13} ight) ight) ight)\ =&Oleft(3n^{frac 23}-3n^{frac 12} ight)\ =&Oleft(n^{frac 23} ight) end{aligned} ]

    所以总时间复杂度为(Oleft(n^{frac 23} ight)),好神奇啊qwq

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    
    const int sq = 316228;
    
    bool notp[sq + 1];
    int mu[sq + 1], prime[sq], num = 0;
    
    void Euler_shai() {
    	mu[1] = 1;
    	for (int i = 2; i <= sq; ++i) {
    		if (!notp[i]) prime[++num] = i, mu[i] = -1;
    		for (int j = 1; j <= num && prime[j] * i <= sq; ++j) {
    			notp[prime[j] * i] = true;
    			if (i % prime[j] == 0)
    				break;
    			mu[prime[j] * i] = -mu[i];
    		}
    	}
    }
    
    ll a, b;
    
    ll cal(ll n) {
    	ll ret = 0;
    	for (int d = 1; 1ll * d * d <= n; ++d)
    		if (mu[d]) {
    			ll up = n / d / d, r = 0;
    			for (int i = 1; 1ll * i * i * i <= up; ++i) {
    				ll up2 = up / i;
    				r += (up2 / i - i) * 3 + 1;
    				for (int j = i + 1; 1ll * j * j <= up2; ++j)
    					r += (up2 / j - j) * 6 + 3;
    			}
    			mu[d] > 0 ? ret += r : ret -= r;
    		}
    	return (ret + n) >> 1;
    }
    
    int main() {
    	scanf("%lld%lld", &a, &b);
    	Euler_shai();
    	printf("%lld
    ", cal(b) - cal(a - 1));
    	return 0;
    }
    
  • 相关阅读:
    hdu 1022 Train Problem I 模拟
    Eclipse plugin开发 —实现语法高亮
    一篇英文版的eclipse插件textEditor资料
    (转)Eclipse常用快捷键
    Eclipse plugin开发 —实现单行与多行注释
    Eclipse plugin开发 —实现文本内容帮助
    CSS borderstyle
    ClassLoader getResource
    javascript add table content from xml
    postgres 当前时间
  • 原文地址:https://www.cnblogs.com/abclzr/p/6775139.html
Copyright © 2011-2022 走看看