zoukankan      html  css  js  c++  java
  • hdu Diophantus of Alexandria(素数的筛选+分解)

    Description

    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
     
     1 #include <string.h>
     2 #include <stdio.h>
     3 #define M 40000
     4 int  prime[50100];
     5 void dabiao()//筛选素数
     6 {
     7     int i,j;
     8     memset(prime,0,sizeof(prime));
     9     for(i=2; i<=M; i++)
    10     {
    11         if(prime[i]==0)
    12         {
    13             for(j=i+i; j<=M; j+=i)
    14             {
    15                  prime[j]=1;
    16             }
    17         }
    18     }
    19 }
    20 int fenjie(int n)//素数因子分解 
    21 {
    22     int i,k,sum=1;
    23     for(i=2; i<=M; i++)
    24         {
    25             if(n==1)
    26             break;
    27             if(prime[i]==0)
    28             {
    29                     k=0;
    30                     while(n%i==0)
    31                     {
    32                         k++;
    33                         n=n/i;
    34                     }
    35                     sum=sum*(2*k+1);
    36             }
    37         }
    38         if(n>1)
    39             sum=sum*3;
    40         return sum;
    41 }
    42 int main()
    43 {
    44 
    45     dabiao();
    46     int n,i,j,t;
    47     scanf("%d",&t);
    48     int p=1;
    49     while(t--)
    50     {
    51         scanf("%d",&n);
    52         printf("Scenario #%d:
    ",p);
    53         printf("%d
    
    ",(fenjie(n)+1)/2);
    54         p++;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Leetcode888. 公平的糖果棒交换
    Leetcode81. 搜索旋转排序数组 II
    Leetcode80. 删除排序数组中的重复项 II
    Leetcode1631. 最小体力消耗路径
    Leetcode57. 插入区间
    Leetcode724. 寻找数组的中心索引
    Leetcode18. 四数之和
    Leetcode110. 平衡二叉树
    Leetcode1128. 等价多米诺骨牌对的数量
    python 集合和深浅copy
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4828672.html
Copyright © 2011-2022 走看看