数论神题——进击的羊角兽

数论神题

进击的羊角兽

题目描述:

求满足 \(a+b|ab(a,b \leq n,a \neq 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\)

\(\because (x+y, x)=1,(x+y, y)=1\)

\(\therefore (x+y)|d\)

$\therefore 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)} \rfloor)\)

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

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

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

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

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

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

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

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

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

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

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

\(\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} \rfloor (d \leq \sqrt{n})$$

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

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

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

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

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

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

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

终于都推完了。

答案记得乘2

分析一下时间复杂度。

\(h(s)\):因为最小值要比大于\(0\),所以\(y \leq \sqrt {s}\),而\(s=\lfloor \frac {t}{z} \rfloor=\lfloor \frac {n}{zd^2} \rfloor\),所以\(s\)最多有\(2\sqrt {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\n", ans*2);
	return 0;
}
posted @ 2015-04-28 21:17  GerynOhenz  阅读(471)  评论(0编辑  收藏  举报