Light OJ 1035 - Intelligent Factorial Factorization(水题)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1035

代码如下:

#include<bits/stdc++.h>
using namespace std;

const int N = 107;
int arr[N][N];

void solve(int cases)
{
    int n;
    scanf("%d", &n);
    printf("Case %d: %d = ", cases, n);
    int first = true;
    for(int i=2; i<=n; ++ i)
    {
        if(arr[n][i] > 0)
        {
            if(first)
            {
                printf("%d (%d)", i, arr[n][i]);
                first = false;
            }
            else
            {
                printf(" * %d (%d)", i, arr[n][i]);
            }
        }
    }
    printf("\n");
}

void init()
{
    memset(arr, 0, sizeof(arr));
    for(int i=2; i<=100; ++ i)
    {
        int t = i;
        for(int j=2; j<=t; ++ j)
        {
            while(t % j == 0)
            {
                arr[i][j] ++;
                t = t / j;
            }
        }
    }
    for(int i=2; i<=100; ++ i)
    {
        for(int j=2; j<=i; ++ j)
            arr[i][j] += arr[i-1][j];
    }
}
int main()
{
    init();
    int t;
    scanf("%d", &t);
    for(int i=1; i<=t; ++ i)
        solve(i);
    return 0;
}

 

posted @ 2017-02-25 08:36  aiterator  阅读(188)  评论(0编辑  收藏  举报