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]; string fac(int n){ string ans; if(n == 0) return "1"; fill(arr, arr + INF, 0); int count = 0, m = n; while(m){ a[++ count] = m % 10; m /= 10; } for(int i = n - 1; i >= 2; i --){ 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; }
|
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Output
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; }
|
Author: o_oyao
License: All articles in this blog are licensed under
CC BY-NC-SA 4.0 unless stating additionally.