1. 程式人生 > >poj 3421 --X-factor Chains(數學、組合)

poj 3421 --X-factor Chains(數學、組合)

 
/*************************************************
*	核心原理:
*	s=p1^a1*p2^a2*p3^a3.....pn^an, 其中p1..pn為素數
**************************************************/
#include<cstdio>
#include<cstring>
#include <cmath>
const int M=(1<<20)+5;
bool is[M];
int prim[M>>1];
int a[100],k;
int getprm(int n)
{
	int i, j, k = 0;
	int s, e = (int)(sqrt(0.0 + n) + 1);
	memset(is, 1, sizeof(is));
	prim[k++] = 2; is[0] = is[1] = 0;
	for (i = 4; i < n; i += 2) is[i] = 0;
	for (i = 3; i < e; i += 2) if (is[i]) {
		prim[k++] = i;
		for (s = i * 2, j = i * i; j < n; j += s)
			is[j] = 0;
		// 因為j是奇數,所以+奇數i後是偶數,不必處理!
	}
	for ( ; i < n; i += 2) if (is[i]) prim[k++] = i;
	return k; // 返回素數的個數
}
int main()
{
	int i,t,k,sum1,j,n;
	__int64 sum2;
	t=getprm(M);
	while(scanf("%d",&n)==1)
	{
		if(is[n])
		{
			printf("1 1\n");
			continue;
		}
		k=0;
		for(i=0;i<t;i++)
		{
			if (n==1) 
				break;
			if (n%prim[i]==0)
			{
				a[k]=0;
				while(n%prim[i]==0) 
					n/=prim[i],a[k]++;	//p[k]=count(ak)
				k++;
			}
		}
		sum1=0;
		for(i=0;i<k;i++)		//sum1 = a1+a2..+an
			sum1+=a[i];
		sum2=1;
		for(i=1;i<=sum1;i++) 
			sum2*=i;
		for(i=0;i<k;i++)		//sum2 = (a1+a2...an)!/(a1!*a2!...*an!) 
			for(j=2;j<=a[i];j++)
				sum2/=j;
			printf("%d %I64d\n",sum1,sum2);
	}
	return 0;
}