UVA324 LA5432 POJ1454 Factorial Frequencies【大數+進制】

Factorial Frequencies

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2927 Accepted: 1252

Description

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can’t just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just 1x2x3x…xn. As she expects to use the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

Input

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don’t bother to process this zero value; just stop your program at that point.

Output

The output format isn’t too critical, but you should make your program produce results that look similar to those shown below.

Sample Input

3
8
100
0
Sample Output

3! –
(0) 0 (1) 0 (2) 0 (3) 0 (4) 0
(5) 0 (6) 1 (7) 0 (8) 0 (9) 0
8! –
(0) 2 (1) 0 (2) 1 (3) 1 (4) 1
(5) 0 (6) 0 (7) 0 (8) 0 (9) 0
100! –
(0) 30 (1) 15 (2) 19 (3) 10 (4) 10
(5) 14 (6) 19 (7) 7 (8) 14 (9) 20

Source
North Central North America 1993

問題鏈接UVA324 LA5432 POJ1454 Factorial Frequencies
問題簡述:給定正整數n,統計n!的各個數字的個數。
問題分析:簡單題不解釋。
程序說明:程序採取簡單的邏輯,計算量會大一些。每次迭代應該記住此時n!的位數再做計算,速度會快,但是程序邏輯略微複雜。
參考鏈接:(略)
題記:(略)

AC的C++語言程序如下:

/* UVA324 LA5432 POJ1454 Factorial Frequencies */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 781 + 1;
const int BASE = 10;       // base
int d[N], f[BASE];

int setDigit(int n)
{
    memset(d, 0, sizeof(d));
    d[0] = 1;
    for(int i = 2; i <= n; i++) {
        int c = 0;      // 進位
        for(int j = 0; j < N; j++) {
            int v = d[j] * i + c;       // 計算乘積
            d[j] = v % BASE;
            c = v / 10;
        }
    }
    int len;
    for(len = N - 1; len >= 0; len--)
        if(d[len]) break;
    return len + 1;
}

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        memset(f, 0, sizeof(f));

        int len = setDigit(n);
        for(int i = 0; i < len; i++)
            f[d[i]]++;

        printf("%d! --\n", n);
        printf("   (0)%5d    (1)%5d    (2)%5d    (3)%5d    (4)%5d\n", f[0], f[1], f[2], f[3], f[4]);
        printf("   (5)%5d    (6)%5d    (7)%5d    (8)%5d    (9)%5d\n", f[5], f[6], f[7], f[8], f[9]);
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章