BZOJ 4459: [Jsoi2013]丢番图 简单数学

title

BZOJ 4459

LUOGU 5253

Description

求方程
\[ F:\frac{1}{x}+\frac{1}{y}=\frac{1}{n} \]

本质不同的整数解的个数,\(n\leqslant 10^{14}\)

analysis

这题还是很有趣的哈。

先将方程进行变形:
\[ F\Longrightarrow\frac{x+y}{xy}=\frac{1}{n}\\ \Longrightarrow n(x+y) = xy\\ \Longrightarrow nx+ny-xy=0\\ \Longrightarrow (n-x)y+nx=0\\ \Longrightarrow (n-x)y-(n-x)n+n^2=0\\ \Longrightarrow (n-x)(y-n)=-n^2\\ \Longrightarrow (n-x)(n-y)=n^2 \]

考虑对于给定的 \(n\in\N^+\) ,方程 \(F\) 的解 \((x,y)\) 的个数,对应了 \((n-x,n-y)\) 的个数,所以,我们要求的东西就变成了 有多对 \((a,b)\) 使得 \(ab=n(a\leqslant b)\) ,所以可以发现这个问题等价于求出 \(d(n^2)\)

由于 \(n^2\leqslant 10^{28}\) ,直接素因数分解的话,时间复杂度会爆炸,所以转换成计算 \(n\) 的因子数,累加一下贡献即可。

所以时间复杂度就是判断素数的时间复杂度了:\(O(\sqrt{n})\)

code

#include<bits/stdc++.h>

typedef long long ll;

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, ch=getchar();
        while (!isdigit(ch) && ch^'-') ch=getchar();
        if (ch=='-') f=-1, ch=getchar();
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

int main()
{
    ll n;read(n);
    ll sum=1, cnt=0;
    for (ll i=2; i*i<=n; ++i)
        if (n%i==0)
        {
            cnt=0;
            while (n%i==0) ++cnt, n/=i;
            sum*=cnt<<1|1;
        }
    if (n>1ll) sum*=3;
    write((sum+1)>>1,'\n');
    IO::flush();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/G-hsm/p/11436547.html