【P5253】丢番图【数论,数学】

题目大意:

题目链接:https://www.luogu.org/problem/P5253
给出nn,求1x+1y=1n\frac{1}{x}+\frac{1}{y}=\frac{1}{n}本质不同的解。


思路:

樱花 那道题基本一样,所以很快就推出来了。

通分
xyx+y=n\frac{xy}{x+y}=n
移项
n(x+y)+xy=0-n(x+y)+xy=0
配方
n2n(x+y)+xy=n2n^2-n(x+y)+xy=n^2
(nx)(ny)=n2(n-x)(n-y)=n^2
所以如果我们n2n^2分解质因数即可。然后两两配对。
n=c1p1×c2p2×...×cnpnn=c_1^{p_1}\times c_2^{p_2}\times ...\times c_n^{p_n},则n=c12p1×c22p2×...×cn2pnn=c_1^{2p_1}\times c_2^{2p_2}\times ...\times c_n^{2p_n}
时间复杂度O(n)O(\sqrt{n})


代码:

#include <cstdio>
using namespace std;
typedef long long ll;

const int N=1e7+10;
ll n,tot,ans,c[N];

int main()
{
	scanf("%lld",&n);
	for (ll i=2;i*i<=n;i++)
		if (!(n%i))
		{
			tot++;
			for (;!(n%i);n/=i)
				c[tot]++;
		}
	if (n>1) c[++tot]++;
	ans=1;
	for (int i=1;i<=tot;i++)
		ans*=(c[i]*2+1);
	printf("%lld",(ans+1)/2LL);
	return 0;
}
posted @ 2019-11-05 08:09  全OI最菜  阅读(189)  评论(0编辑  收藏  举报