hdu1299-Diophantus of Alexandria(分解素因子)

http://acm.hdu.edu.cn/showproblem.php?pid=1299

题意:给定一个数值n,求满足1/ x + 1/ y = 1 /n的x,y 的组数,又因为x,y一定大于n 。所以设y = n + k ;

带入方程得:x = n * n / k + n ;

现在就变成了求x的值。又因为n一定大于k,所以转换成求n * n的分解数;

因为n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

sum ( n)= ( 1 + e1 ) * ( 1 +e2 ) * .........* ( 1 +en );

sum (n * n) = ( 1 + e1 ) * (1 + e2 ) *......* ( 1 + en ) ; 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip>

using namespace std;


#define MAX_PRIME 31700
#define PRIME_NUM 3500

int Primes[ PRIME_NUM + 10 ] ;

int _Count = 0  ;
int GetPrimes( )
{
	unsigned char  *PrimeBuffer = ( unsigned char * ) malloc( sizeof( unsigned char ) * ( MAX_PRIME + 10 ) ) ;
	int i , j  ;
	
	memset( Primes , 0 , sizeof( int ) *PRIME_NUM ) ;
	memset( PrimeBuffer , 0 , sizeof( unsigned char) * MAX_PRIME ) ;
	
	
	for( i = 2 ; i < MAX_PRIME ; i++ )
	{
		if( PrimeBuffer[ i ] == 0 )
			Primes[ _Count++ ] = i ;
		for( j = 0 ; j < _Count && i * Primes[ j ] <= MAX_PRIME ; j++ )
		{
			PrimeBuffer[ i * Primes[ j ] ] = 1 ;
		 	if( i % Primes[ j ] == 0 )
			 	 break ;
		}
	}
	free( PrimeBuffer ) ;
	return _Count ;
}

int main()
{
	GetPrimes();
	int Case , n , num , sum , temp;
	cin >> Case ;
	temp = 0 ;
	while( Case-- )
	{ 
		cin >> n ;
		sum = 1 ;
		for( int i = 0 ; i < _Count ; ++i )
		{
			int  flag =  ( int )sqrt( n ) + 1 ;
			if( Primes[ i ] > flag )
				break ;
			num = 0 ;
			while( n % Primes[ i ] == 0 )
			{
				num++ ;
				n /= Primes[ i ] ;
			}
			sum *= ( 1 + 2 * num ) ;
		}
		if( n > 1 )
			sum *= 3 ;
		cout << "Scenario #" << ++temp << ":" << endl ;
		cout << ( sum + 1 ) / 2 << endl << endl ;
	}
	return 0 ;
}


你可能感兴趣的:(hdu1299-Diophantus of Alexandria(分解素因子))