LightOJ 1035 Intelligent Factorial Factorization

2082 단어 inputeachfun
Description
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




Sample Output
Case 1: 2 = 2 (1)
Case 2: 3 = 2 (1) * 3 (1)
Case 3: 6 = 2 (4) * 3 (2) * 5 (1)
제목이 N!분해질 인수는 3=2^1*3^1
#include<stdio.h>
#include<string.h>
int select(int prime[])
{
    char flag[126]={0};
    int i,j;
    for(i=2;i<13;i++)
    {
        for(j=i*2;j<126;j+=i)
        {
            flag[j]=1;
        }
    }
    for(i=2,j=0;i<126;i++)
    {
        if(!flag[i])
        prime[j++]=i;
    }
    return j;
}
int fun(int n,int prime[],int count[],int prinum)
{
    int number[126],i,j;
    for(i=1;i<=n;i++)
        number[i]=i;
    for(i=0;i<prinum;i++)
    {
        if(prime[i]<=n)
        number[prime[i]]=1,count[i]++;
    }
    for(i=1;i<=n;i++)
    {
        for(j=0;j<prinum;j++)
        {
            while(number[i]%prime[j]==0)
            {
                number[i]/=prime[j];
                count[j]++;
            }
        }
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int prime[31],count[126],prinum,n,t=1,i,tCase;
    prinum=select(prime);
    scanf("%d",&tCase);
    while(tCase--)
    {
        scanf("%d",&n);
        memset(count,0,sizeof(count));
        fun(n,prime,count,prinum);
        printf("Case %d: %d = ",t++,n);
        for(i=0;count[i];i++)
        {
            printf("%d (%d)",prime[i],count[i]);
            if(count[i+1])printf(" * ");
        }
        printf("
"); } return 0; }

좋은 웹페이지 즐겨찾기