324-factorial Frequencies
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=260
In a attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological S to her customers. She has been able to convince them this frequency of occurrence of the digits in the decimal representation of Factori ALS bear witness to their futures. Unlike palm-reading, however, she can ' t just conjure up this frequencies, so she has employed all these determine Es.
Recall that definition of n! (That's, n factorial) is just. As she expects to use either the "Day of the" week, the day of the month, or the "year of" as the value ofn, you must is able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which ha s 781 digits.
Input and Output
The input data is simply a list of integers to which the digit counts are. All of these input values would be less than or equal to 366 and greater than 0, except for the last integer, which would be Zero. Don ' t bother to process this zero value; Just stop your program at this point. The output format isn ' t too critical, but you should do your program produce results so look similar to those shown is Low.
Madam Phoenix is forever (or longer) in your debt; She might even give for a trip if your do your job well!
Sample Input
3
8
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) (1) ( 2) (3) ( 4)
(5) ( 6) (7) 7 (8) (9) 20
Complete code:
/*0.022s*/#include <bits/stdc++.h> using namespace std;
const int MAXN = 800;
Char outputstr[maxn];///This string as a copy of s in bign for output int dig[10];
struct Bign {int len, S[MAXN];
Bign () {memset (s, 0, sizeof (s));
len = 1;
} bign (int num) {*this = num;
} bign (const char* num) {*this = num;
} bign operator = (const int num) {char S[MAXN];
sprintf (S, "%d", num);
*this = s;
return *this;
} bign operator = (const char* num) {len = strlen (num);
for (int i = 0; i < len; i++) S[i] = num[len-i-1] & 15;
return *this; ///Output Const char* str () const {if (len) {for (int i = 0; i < Len
i++) outputstr[i] = ' 0 ' + s[len-i-1]; Outputstr[len] =' I ';
else strcpy (Outputstr, "0");
return outputstr;
///to the leading 0 void clean () {while (len > 1 &&!s[len-1]) len--;
///plus bign operator + (const bign& b) const {bign C;
C.len = 0;
for (int i = 0, g = 0; G | | | i < MAX (Len, B.len); i++) {int x = g;
if (i < len) x + = S[i];
if (I < B.len) x + = B.s[i];
c.s[c.len++] = x% 10;
g = X/10;
return C;
///minus bign operator-(const bign& B) const {bign C;
C.len = 0;
for (int i = 0, g = 0; i < len; i++) {int x = s[i]-G;
if (I < b.len) x-= B.s[i];
if (x >= 0) g = 0;
else {g = 1;
x + 10;
} c.s[c.len++] = x;
} C.clean ();
return C;
///Multiply bign operator * (const bign& b) const {bign C;
C.len = len + B.len; for (int i = 0; i < len; i++) for (int j = 0; J < B.len; J +) C.s[i + j] = = S[i] * b
. S[j];
for (int i = 0; i < c.len-1 i++) {c.s[i + 1] + = c.s[i]/10;
C.s[i]%= 10;
} C.clean ();
return C;
}///except bign operator/(const bign &b) const {bign ret, cur = 0;
Ret.len = Len;
for (int i = len-1 i >= 0; i--) {cur = cur * 10;
Cur.s[0] = s[i];
while (cur >= b) {cur = b;
ret.s[i]++;
} Ret.clean ();
return ret; ///modulo, Yu///this article URL address:http://www.bianceng.cn/Programming/sjjg/201410/45518.htm bign operator% (const bign &b) Const {BIG
n C = *this/b;
return *this-c * b;
BOOL operator < (const bign& b) Const {if (len!= b.len) return Len < B.len;
for (int i = len-1 i >= 0; i--) if (S[i]!= b.s[i]) return s[i] < b.s[i];
return false;
BOOL operator > (const bign& B) Const {return B < *this; BOOL operator <= (const bign& B) Const {return! (
b < *this); BOOL operator >= (const bign &b) Const {return! (
*this < b); BOOL operator = = (CONST bign& b) Const {return! b < *this) &&!
(*this < b);
BOOL operator!= (const bign &a) const {return *this > A | | *this < A; BigN operator + = (const bign &a) {*this = *this + A;
return *this;
} bign Operator-= (const bign &a) {*this = *this-a;
return *this;
} bign operator *= (const bign &a) {*this = *this * A;
return *this;
} bign operator/= (const bign &a) {*this = *this/a;
return *this;
} bign operator%= (const bign &a) {*this = *this% A;
return *this;
}} a[367];
int main () {int i, n, Len;
A[1] = 1;
for (i = 2; i < 367 ++i) a[i] = a[i-1] * i;
while (scanf ("%d", &n), N) {memset (dig, 0, sizeof (DIG));
Len = strlen (A[n].str ());
for (i = 0; i < len; ++i) ++dig[outputstr[i] & 15]; printf ("%d!
--\n ", n);
for (i = 0; i < ++i) { printf ("(%d)%5d", I, dig[i]);
if (i = = 4) Putchar (10);
} putchar (10);
return 0; }