2012-01-12 17:23:57Morris

[UVA] 10220 - I Love Big Numbers !

 I Love Big Numbers !

The Problem

A Japanese young girl went to a Science Fair at Tokyo. There she met with a Robot named Mico-12, which had AI (You must know about AI-Artificial Intelligence). The Japanese girl thought, she can do some fun with that Robot. She asked her, "Do you have any idea about maths ?"."Yes! I love mathematics", The Robot replied.

"Okey ! Then I am giving you a number, you have to find out the Factorial of that number. Then find the sum of the digits of your result!. Suppose the number is 5.You first calculate 5!=120, then find sum of the digits 1+2+0=3.Can you do it?"

"Yes. I can do!"Robot replied.

"Suppose the number is 100, what will be the result ?".At this point the Robot started thinking and calculating. After a few minutes the Robot head burned out and it cried out loudly "Time Limit Exceeds".

The girl laughed at the Robot and said "The sum is definitely 648".

"How can you tell that ?" Robot asked the girl. "Because I am an ACM World Finalist and I can solve the Big Number problems easily." Saying this, the girl closed her laptop computer and went away.

Now, your task is to help the Robot with the similar problem.

 

The Input

The input file will contain one or more test cases.

Each test case consists of one line containing an integers n (n<=1000).

The Output

For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2^31-1.

Sample Input

5
60
100 

Sample Output

3
288
648

--------------------------------------------------------------------------------------------------------------------

作法 : 大數運算

#include<stdio.h>
int P[1001][2600] = {}, Ans[1001] = {};
int Build(int P[][2600]) {
    int i, j, last = 0;
    P[0][0] = 1, Ans[0] = 1;
    P[1][0] = 1, Ans[1] = 1;
    for(i = 2; i <= 1000; i++) {
        for(j = 0; j <= last; j++) {
            P[i][j] += P[i-1][j]*i;
            if(P[i][j] >= 10) {
                P[i][j+1] += P[i][j]/10;
                P[i][j] %= 10;
                if(j+1 > last)    last = j+1;
            }
            Ans[i] += P[i][j];
        }
    }
}
int main() {
    Build(P);
    int n;
    while(scanf("%d", &n) == 1)
        printf("%d\n", Ans[n]);
    return 0;
}