light oj 1035

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

Output for Sample Input

3

2

3

6

Case 1: 2 = 2 (1)

Case 2: 3 = 2 (1) * 3 (1)

Case 3: 6 = 2 (4) * 3 (2) * 5 (1)

Notes

The output for the 3rd case is (if we replace space with '.') is

Case.3:.6.=.2.(4).*.3.(2).*.5.(1)

题意:阶乘素因子分解,

分析:数据不是很大没有什么难度,直接除就好

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
bool book[102];
int s=0, pri[102], num[102];

void inin()
{
    memset(book, true, sizeof(book));
    for(int i=2; i<=100; i++)
    {
        if(book[i])
        {
            pri[s++]=i;
            for(int j=i+i; j<=100; j+=i)
                book[j]=false;
        }
    }
}
int main()
{
    inin();//打一个素数小表
    int T, t=1, n;
    scanf("%d", &T);
    while(T--)
    {
        memset(num, 0, sizeof(num));
        scanf("%d", &n);
        for(int i=0; i<s; i++)
        {
            int ss=n;
            while(ss>0)
                num[pri[i]]+=ss/pri[i],ss/=pri[i];//阶乘素因子个数可以直接除得到
        }
        printf("Case %d: %d = 2 (%d)", t++, n, num[2]);
        for(int i=3; i<=100; i++)
            if(num[i]!=0)
            printf(" * %d (%d)", i, num[i]);
        printf("
");
    }
}
原文地址:https://www.cnblogs.com/zhulei2/p/8206625.html