首页 > ACM题库 > HDU-杭电 > HDU 1299 Diophantus of Alexandria-数论-[解题报告] C++
2013
12-04

HDU 1299 Diophantus of Alexandria-数论-[解题报告] C++

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?

输入:

The first line contains the number of scenarios. Each scenario consists of one line containing a single number n (1 ≤ n ≤ 10^9).

输出:

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.

样例输入:

2
4
1260

样例输出:

Scenario #1:
3

Scenario #2:
113

        题目大意:给你一个数N 求 1/X+1/Y=1/N 中X ,Y 有几种组合。

  分析思路: 由题意可知 X〉N,Y〉N。  我们可令 Y=N+ K(K为正整数)。

                                化简可得 X= (N* N)/ K + N 。   X,Y 为正整数 。 

                                所以呢, 只需N* N的因子数即可。

                               知道了这个,之后就直接 A 就行。

         代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define Len 50000

bool a[Len];
int b[Len];
int k;
void Isprime()
{
     int i,j;
     a[2]=true;
     k=1;
     b[0]=2;
     for(i=3;i<Len;i++)
     {
                       if(i&1)
                       a[i]=true;
                       else
                       false;
     }
     for(i=3;i<Len;i++)
     {
                       if(a[i])
                       {
                               b[k]=i;
                               k++;
                               for(j=i+i;j<Len;j+=i)
                                                    a[j]=false;
                       }
     }
}
int main()
{
    Isprime();
    int x,n,i,j,sum,m=1,t;
    scanf("%d",&n);
    while(n--)
    {
              j=0;
              sum=1;
              scanf("%d",&x);
              for(i=0;i<k;i++)
              {
                    t=0;
                    if(x<b[i])
                    break;
                    while(x%b[i]==0 )
                    {
                           x=x/b[i]; 
                           t++;        
                    }
                    sum *= (t*2+1);
                    j++;
              }
              printf("Scenario #%d:\n",m);
              m++;
              if(x>1)  // 可能 存在 一个 素数 X . 
              sum*=3;
              printf("%d\n\n",(sum+1)/2);
    }
    return 0;
}


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。