zoukankan      html  css  js  c++  java
  • 数论神题——进击的羊角兽

    数论神题

    进击的羊角兽

    题目描述:

    求满足 (a+b|ab(a,b leq n,a eq b))的有序数对((a,b))的个数。

    solution

    ((a,b)=d , (a < b leq n)),则$ a=xd , b=yd , ( x < y )$

    (a+b|ab)

    (=(x+y)d|xyd^2)

    (ecause (x+y, x)=1,(x+y, y)=1)

    ( herefore (x+y)|d)

    $ herefore x < y leq sqrt{n} $

    (d=z(x+y)),则(a=xz(x+y) , b=yz(x+y))

    即原问题为求数对((x, y, z)( x < y leq sqrt{n}, z leq lfloor frac {n}{y(x+y)} floor))

    $$Ans=sum_{x < y} sum_{(x, y)=1} lfloor frac {n}{y(x+y)} floor$$

    $$=sum_{x < y} sum_{d=(x, y)}varepsilon(d) lfloor frac {n}{y(x+y)} floor$$

    $$=sum_{x < y} sum_{d|(x,y)} mu(d) lfloor frac {n}{y(x+y)} floor$$

    $$=sum_{x < y,d|x,d|y} mu(d) lfloor frac {n}{y(x+y)} floor$$

    (x=x'd, y=y'd)

    $$=sum_{x' < y'} mu(d) lfloor frac {n}{y'(x'+y')d^2} floor$$

    为了方便,下面的(x',y' ext 用回 x,y ext 表示)

    $$=sum_{x < y} mu(d) lfloor frac {n}{y(x+y)d^2} floor (d leq sqrt{n})$$

    现在就来解释一下上面的(varepsilon(), mu())

    这个是莫比乌斯函数的性质。

    这样只有当((x,y)=1)时,(lfloor frac {n}{y(x+y)} floor) 才会被算进去。

    (mu(n))很容易就可以算出来,显然当(n=1)(mu(1)=1),对于一个数(n),它的所有约数的(mu())的和为(0),而(n)的其中一个约数就是(n),小于(n)(mu())又已经算出来了,就可以把(mu(n))算出来。

    $$sum_{x < y} mu(d) lfloor frac {n}{y(x+y)d^2} floor (d leq sqrt{n})$$

    $$f(t)=sum_{x < y} lfloor frac {t}{y(x+y)} floor$$

    $$Ans=sum_{x < y} mu(d)f(frac {n}{d^2})$$

    $$f(t)=sum_{x < y} lfloor frac {t}{y(x+y)} floor$$

    $$=sum_{x < y, z leq lfloor frac {t}{y(x+y)} floor} 1$$

    $$=sum_{x leq y-1, y(x+y) leq lfloor frac {t}{z} floor} 1$$

    $$=sum_{x leq y-1, x leq lfloor frac {t}{zy} floor -y} 1$$

    [f(t)=sum h(lfloor frac {t}{z} floor) ]

    终于都推完了。

    答案记得乘2

    分析一下时间复杂度。

    (h(s)):因为最小值要比大于(0),所以(y leq sqrt {s}),而(s=lfloor frac {t}{z} floor=lfloor frac {n}{zd^2} floor),所以(s)最多有(2sqrt {n})个,时间复杂度为:

    $$int_{0}^{sqrt {n}} (sqrt {x} + sqrt { frac {n}{x}} ) mathrm{d}x$$

    (f(s)):分析同上,一个(f(s))需要(sqrt {s})的时间

    (Ans):枚举(d)(sqrt {n}),算(f(s))需要(sqrt {s})的时间,总的时间复杂度为:

    $$int_{0}^{sqrt {n}} (sqrt {x} + sqrt { frac {n}{x}} ) mathrm{d}x$$

    预处理(h(s)),所以整个算法的时间为:

    $$int_{0}^{sqrt {n}} (sqrt {x} + sqrt { frac {n}{x}} ) mathrm{d}x$$

    $$=frac {8}{3} n^{ frac {3}{4}}$$

    贴代码:

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    #include <map>
    #include <deque>
    #include <queue>
    using namespace std;
    
    typedef long long LL;
    const int maxn=int(1e6)+100;
    
    LL n, ans;
    int qn;
    LL u[maxn], h[maxn], hv[maxn];
    
    void getu()
    {
    	for (int i=1; i<=qn; ++i) u[i]=1;
    	for (int i=2; i<=qn; ++i)
    	{
    		u[i]=-u[i];
    		for (int j=2; i*j<=qn; ++j) u[i*j]+=u[i];
    	}
    }
    LL geth1(LL num)
    {
    	int cut=(sqrt(1+8*num)+1)*0.25;
    	int fi=(int)floor(sqrt(num));
    	LL ret=LL(cut-1)*cut/2-(LL)(cut+1+fi)*(fi-cut)/2;
    	for (int i=cut+1; i<=fi; ++i)
    		ret+=num/i;
    	return ret;
    }
    void geth()
    {
    	for (int i=1; i<=qn; ++i)
    	{
    		h[i]=geth1(i);
    		hv[i]=geth1(n/i);			
    	}
    }
    LL getf(LL num)
    {
    	LL f=0;
    	int fi=(int)floor(sqrt(num));
    	for (int i=1; i<=fi; ++i)
    		f+=h[i]*(num/i-num/(i+1));
    	for (int i=1; i<=fi; ++i)
    	{
    		if (num / i <= fi) continue;
    		if (num/i>sqrt(n)) f+=hv[n/(num/i)];
    		else f+=h[num/i];
    	}
    	/*
    	if (LL(sqrt(num))*LL(sqrt(num))==num)
    		f-=h[LL(sqrt(num))];
    		*/
    	return f;
    }
    void solve()
    {
    	getu();
    	geth();
    	for (int i=1; i<=qn; ++i)
    		ans+=u[i]*getf(n/((long long)i*i));
    }
    int main()
    {
    	freopen("nixgnoygnay.in", "r", stdin);
    	freopen("nixgnoygnay.out", "w", stdout);
    	scanf("%I64d", &n);
    	qn=(int)floor(sqrt(n));
    	solve();
    	printf("%I64d
    ", ans*2);
    	return 0;
    }
    
  • 相关阅读:
    Spring5.x与日志框架的整合
    日常问题列表及解答
    Spring国际化i18n
    Spring MVC 功能支持
    Spring自定义XML配置扩展
    Spring 常用的一些工具类
    Spring重要的类和接口
    Spring内置的定时任务调度@Scheduled
    【第八章 时序检查 上】静态时序分析圣经翻译计划
    【第七章 配置STA环境 下】静态时序分析圣经翻译计划
  • 原文地址:https://www.cnblogs.com/GerynOhenz/p/4464017.html
Copyright © 2011-2022 走看看