160-factors and Factorials
Time limit:3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=96
Topic Point this: http://uva.onlinejudge.org/external/1/160.pdf
Train of thought: Traversal 1~n calculate the number of qualitative factors.
If you want to be faster, use [n/p]+[n/p^2]+[n/p^3]+...+1 in O (log n/log p) time to count the number of n! p. (although this question is too small, the two methods are not very different in time)
Complete code:
/*0.016s*/#include <cstdio> #include <cmath> #include <cstring> int prime[100], C, a
NS[100];
BOOL vis[110];
inline void Create_prime () {int i, J;
for (i = 2; I <= ++i) if (!vis[i]) {prime[c++] = i;
for (j = i * I; J < A; J + = i) vis[j] = true;
for (; i < A; ++i) if (!vis[i]) prime[c++] = i;
int main (void) {create_prime ();
int n, temp;
while (scanf ("%d", &n), N) {memset (ans, 0, sizeof (ans)); printf ("%3d!
= ", n);
for (int i = 2; I <= n; i++) {temp = i;
for (int j = 0; J < c && Prime[j] <= i; j +) {while (temp% prime[j] = 0)
{++ans[prime[j]];
Temp/= prime[j]; }
for (int i = 0, j = 0; Prime[i] <= N; i++, J + +) {if J%
(= = = 0 && j) printf ("\ n");
printf ("%3d", Ans[prime[i]);
} putchar (' \ n ');
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/