Given an integer N, you have to prime factorize N! (factorial N).
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case contains an integer N (2 ≤ N ≤ 100).
Output
For each case, print the case number and the factorization of the factorial in the following format as given in samples.
Case x: N = p1 (power of p1) * p2 (power of p2) * ...
Here x is the case number, p1, p2 ... are primes in ascending order.
Sample Input
3
2
3
6
Sample Output
Case 1: 2 = 2 (1)
Case 2: 3 = 2 (1) * 3 (1)
Case 3: 6 = 2 (4) * 3 (2) * 5 (1)
题意:给出一个数N,求N的阶层中有哪些质数和每种质数的个数。
题解:给出的数据虽小,但是对于求阶层,好像求到十几就会爆的,所以,这道题我采用的方式是先把素数打表,
因为素数的排序方式就是从小到大的,所以直接从小到大进行数N的阶层的分解。
同时开个一维数组来记录分解成每个素数的个数。
#include<stdio.h>
#include<string.h>
bool su[11010];
int sum[1010];
void isprime()//素数打表
{
memset(su,0,sizeof(su));
for(int i=2; i<=110; i++)
{
if(su[i]==0)
{
for(int j=2*i; j<=110; j+=i)
su[j]=1;
}
}
su[0]=su[1]=2;
}
int main()
{
int t,o=1;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(sum,0,sizeof(sum));
int num=1;
int m=n;
while(n>1)//求阶层的过程直接分解
{
num=num*n;
for(int j=2; j<=n; j++)
{
if(su[j]==0)
{
while(num%j==0)//分解成质数的指数形式
{
sum[j]++;
num=num/j;
}
}
}
if(num!=0)
num*=num;
else if(num==0)//比如6可以分解成2,3,然后出现num==0,如果不初始化为1,num将一
//直为零,求阶层的过程会无意义
num=1;
n--;
}
printf("Case %d: %d = ",o++,m);
for(int i=1; i<=110; i++)
{
if(sum[i]==0)
continue;
else
{
if(i==2)
printf("%d (%d)",i,sum[i]);
else
printf(" * %d (%d)",i,sum[i]);
}
}
printf("\n");
}
return 0;
}