天天看点

PE 110 Diophantine reciprocals II (数论:分式个数)(加强版)(dfs)

PE 110题目

PE 108 题解

从PE 108 我们可以知道,分式 1a+1b=1n 的个数 就是 1+d(n2)2 ,其中 d(n) 是n的约数的个数。、

而 PE 110 只是PE 108的加强版。用PE 108的方法 换个方式就可以了。

比如:

n=2p13p25p3......

d(n2)=N=∏i=1(2pi+1)

所以我们可以知道样例是怎么来的。

样例:

1260=22325171

d(n2)=N=5∗5∗3∗3=225

综合 1+d(n2)2 ,所以答案就是 225+12=113 。

我们要求的,只是倒过来求 n <script type="math/tex" id="MathJax-Element-10">n</script> 而已。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll primes[]={, , , , , , , , , , , , , , ,};
ll ans;
void dfs(ll down, ll upper, ll build, ll cost)
{
    if(cost > ans)return;
    if(build > ) //4000000 * 2 -1
    {
        ans=min(ans, cost);
        return;
    }
    if(down>=)return;

    for(ll i = upper;i>=;i--)
    {
        ll tmp=;
        for(int j =  ; j <= i ; j++)
        {
            tmp*=primes[down]; 
        }

        if((double) tmp*cost > (double)ans) continue;

        dfs(down + , i , build * (  * i +  ), tmp * cost);
    }
}

int main()
{
    ans=;
    dfs(, , , );
    printf("%lld\n", ans);
    return ;
}