ORIGIN

Big N!

ACM 2 mins329 words

Using C/C++ we can’t calculate very big number of prime factors. So we use string to store it.

Template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#define INF 1000010
int arr[INF]; // store each digit
string fac(int n){
string ans;
if(n == 0) return "1"; // the factorial of 0 is 1;
fill(arr, arr + INF, 0); // initial the array with 0
int count = 0, m = n; // count is length of digits
while(m){
a[++ count] = m % 10; // calculate each digit
m /= 10;
}
for(int i = n - 1; i >= 2; i --){ // starts the factorial calculation
int t = 0;
for(int j = 1; j <= count; j ++){
arr[j] = arr[j] * i + t;
t = arr[j] / 10;
arr[j] %= 10;
}
while(t){
arr[++ count] = t % 10;
t /= 10;
}
}
while(!arr[count]) count --;
while(count >= 1) ans += arr[count --] + '0';
return ans;
}

N! (HDU 1042)

Problem Description

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

Input

One N in one line, process to the end of file.

Output

For each N, output N! in one line.

Sample Input

1
2
3
1
2
3

Sample Output

1
2
3
1
2
6

AC Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>

using namespace std;

const int L = 100005;

int a[L];

string fac(int n){
string ans;
if(n == 0) return "1";
fill(a, a + L, 0);
int s = 0, m = n;
while(m){
a[++ s] = m % 10;
m /= 10;
}
for(int i = n - 1; i >= 2; i --){
int w = 0;
for(int j = 1; j <= s; j ++){
a[j] = a[j] * i + w;
w = a[j] / 10;
a[j] %= 10;
}
while(w){
a[++ s] = w % 10;
w /= 10;
}
}
while(!a[s]) s --;
while(s >= 1) ans += a[s --] + '0';
return ans;
}
int main () {
int n;
while(cin >> n){
cout << fac(n) << endl;
}
return 0;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|