您的位置:首页 > 运维架构

hdu Diophantus of Alexandria(求因子的个数)

2015-04-17 09:55 267 查看

Diophantus of Alexandria

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2569 Accepted Submission(s): 975



Problem 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




Source
TUD Programming Contest 2006

题目分析:

首先对于式子1/x+1/y = 1/n,那么我们设y为a,那么该式成立的充要条件是n*a/(a-n)能整除,那么设i=a-n,因为a>n,所以i一定是正数,那么n*(i+n)/i=n+n*n/i,只需判断i是否为n×n的因子,对应到这道题就是求n*n的因子个数,也就是(各个素因子的个数+1) 相乘,各个因子的个数又是n的各个素因子的个数的二倍,所以先筛出sqrt(n)以内的素数,然后判断遍历素因子,剔除出去,因为最多只可能出现一个超过sqrt(n)的素因子,所以求取时剔除到最后只可能是1或大于sqrt(n)的那个素数,特判一下即可,因为求的是y,而x,y的位置可以互换,且算作一种,所以要除2,但因为可能出现同一个数组合,所以要加1除以2

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#define MAX 100007

using namespace std;

typedef long long LL;
int t;
LL n;

bool check ( LL num )
{
    if ( num < 2 ) return false;
    for ( LL i = 2 ; i*i<=num ; i++ )
       if ( num%i == 0 ) return false;
    return true; 
}

LL prime[MAX];
int vis[MAX];
int cnt;

void init ( )
{
    for ( int i = 1 ; i < MAX ; i++ ) vis[i] = 1;
    cnt = 0;
    vis[1] = vis[0] = 0;
    for ( LL i = 2 ; i < MAX ; i++ )
    {
        if ( !vis[i] ) continue;
        vis[i] = cnt , prime[cnt++] = i;
        for ( LL j = i*i ; j < MAX ; j += i )
            vis[j] = 0;
    }
}

LL sum;
LL num[MAX];

LL solve ( LL n )
{
    if ( n == 1 ) return 1;
    memset ( num , 0 , sizeof ( num ) );
    sum = 0;
    for ( int i = 0 ; i < cnt && prime[i] <= n ; i++ )
    {
        if ( !(n%prime[i]) ) ++sum;
        while ( !(n%prime[i]) )
            num[sum]++, n/=prime[i];
    }
    if ( n != 1 ) num[++sum]++;
    LL ret = 1;
    for ( int i = 1 ; i <= sum ; i++ )
        ret *= (num[i]*2 + 1);
    return (ret+1)/2;
}

int main ( )
{
    scanf ( "%d" , &t );
    int c = 1;
    init ( );
    while ( t-- )
    {
        scanf ( "%lld" , &n );
        printf ( "Scenario #%d:\n" , c++ );
        printf ( "%lld\n\n" , solve ( n ) );
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: