Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5139
The title means: Give a number n, and find out f (n).
The following rules can be found:
F (1) = 1!
F (2) = 1! * 2!
F (3) = 1! * 2! * 3!
F (n) = 1! * 2! * 3! * .... * (n-1)! * n!
I began to the main puzzle of the direct table super memory is not very understanding, so the practice of the following:
Hyper-memory version (that is, direct count F[n], when input n, output F[N]):
(The original size of the 1E7 array will be such a drop, long experience ~ ~)
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intMOD = 1e9 +7;8 Const intMAXN = 1e7 +2;9 Ten intF[MAXN]; One A intMain () - { - intp =1, S =1; the for(inti =1, j =1; I <= MAXN; i++) - { - while(J <=i) - { +p = 1ll * p * J%MOD; -s = 1ll * s * p%MOD; +J + +; A } atF[i] =s; - } - intN; - while(SCANF ("%d", &n)! =EOF) -printf"%d\n", F[n]); - return 0; in}View Code
So we have to deal with the offline, the eldest brother speaks better.
http://blog.csdn.net/sr_19930829/article/details/41785767
Namely: Unified data input, unified processing, the final unified output.
In order to prevent hyper-memory, open the 1e5-size array to save the answer, because the topic said cases about 100000.
After the input is small to large order, such benefits can ensure that each factorial only calculated once. In order to correspond to the order of input, we need the second of the pair array to save the ordinal.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intMOD = 1e9 +7;8 Const intMAXN = 1e5 +2;9 Tenpair<int,int>PII[MAXN]; One intANS[MAXN]; A - intMain () - { the intCNT =0; - while(SCANF ("%d", &pii[cnt].first)! =EOF) - { -Pii[cnt].second =CNT; +cnt++; - } + ASort (PII, pii+CNT); at intp =1, S =1; - for(inti =0, j =1; I < CNT; i++) - { - while(J <=Pii[i].first) - { -p = 1ll * p * J%MOD; ins = 1ll * s * p%MOD; -J + +; to } +Ans[pii[i].second] =s; - } the for(inti =0; I < CNT; i++) *printf"%d\n", Ans[i]); $ return 0;Panax Notoginseng}
BestCoder21 1002.Formula Problem Solving report