HDU 1299 Diophantus of Alexandria

Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first mathematicians to study equations where variables were restricted to integral values. In honor of him, these equations are commonly called diophantine equations. One of the most famous diophantine equation is x^n + y^n = z^n. Fermat suggested that for n > 2, there are no solutions with positive integral values for x, y and z. A proof of this theorem (called Fermat’s last theorem) was found only recently by Andrew Wiles.

Consider the following diophantine equation:

1 / x + 1 / y = 1 / n where x, y, n ∈ N+ (1)

Diophantus is interested in the following question: for a given n, how many distinct solutions (i. e., solutions satisfying x ≤ y) does equation (1) have? For example, for n = 4, there are exactly three distinct solutions:

1 / 5 + 1 / 20 = 1 / 4
1 / 6 + 1 / 12 = 1 / 4
1 / 8 + 1 / 8 = 1 / 4

Clearly, enumerating these solutions can become tedious for bigger values of n. Can you help Diophantus compute the number of distinct solutions for big values of n quickly?
Input
The first line contains the number of scenarios. Each scenario consists of one line containing a single number n (1 ≤ n ≤ 10^9).
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Next, print a single line with the number of distinct solutions of equation (1) for the given value of n. Terminate each scenario with a blank line.
Sample Input
2
4
1260
Sample Output
Scenario #1:
3

Scenario #2:
113

题意:给出一个n,问1 / x + 1 / y = 1 / n where x, y, n ∈ N+ (1)有几组解

思路
原式化简:
xy=n*(x+y)
xy-nx-ny=0;
两边同时加上n^2
n^2+xy-nx-ny= n ^2
(n-x)(n-y)=n^2
所以结果就是求n^2的因子数
有唯一存在定理知道
因子数是(1+e1)(1+e2)(1+e3)(1+er)
这里是nn
所以因子数就是ans=(1+2
e1)(1+2e2)(1+2*er)
还要注意,这里要求了x>=y(所以最后还要除以2)
还有,当n是质数时公式乘以3

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 100000 + 5;
bool prime[N];
int p[N], tot;
void su()
{
    for(int i = 2; i < N; i ++)
     prime[i] = true;
    for(int i = 2; i < N; i++)
        {
        if(prime[i]) p[tot ++] = i;
        for(int j = 0; j < tot && i * p[j] < N; j++)
        {
            prime[i * p[j]] = false;
            if(i % p[j] == 0) break;
        }
    }
}
int go(ll x)
{
    ll sum=1;
    for(int i=0;p[i]*p[i]<=x;i++)
    {
        if(x%p[i]==0)
        {

            int s=0;
            while(x%p[i]==0)
            {
                s++;
                x/=p[i];
            }
            sum*=2*s+1;
        }
    }
    if(x>1)
        sum*=3;
    return (sum+1)/2;
}
int main()
{
    su();
    int t;
    scanf("%d",&t);
   for(int i=1;i<=t;i++)
    {
        ll n;
        scanf("%lld",&n);
        ll k;
        k=go(n);
        printf("Scenario #%d:\n",i);
        printf("%lld\n\n",k);
    }
}

HDU 1299 Diophantus of AlexandriaHDU 1299 Diophantus of Alexandria weixin_43244265 发布了251 篇原创文章 · 获赞 14 · 访问量 5952 私信 关注
上一篇:The Preliminary Contest for ICPC Asia Yinchuan 2019


下一篇:A. XOR Equation