hdu1042【大数阶乘】

来源:互联网 发布:淘宝官方app客户端下载 编辑:程序博客网 时间:2024/06/03 14:53

N!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 79143 Accepted Submission(s): 23137

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

Sample Output
1
2
6

大数的阶乘,10000的阶乘为35000多位数。
开个3000的数组,数组里的每个数储存13位数,就足够了
这算是10万亿进制了吧233333
代码:

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#define MST(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 9999using namespace std;long long a[10005][3001];void deal(){    MST(a, 0);    a[0][1] = 1;    for (int i = 1; i <= 10000; i++)    {        long long r = 0;        for (int j = 1; j <= 3000; j++)        {            a[i][j] = a[i - 1][j] * i + r;            if (a[i][j] < 0)                printf("\n");            r = 0;            if (a[i][j] > 10000000000000)            {                r = a[i][j] / 10000000000000;                a[i][j] %= 10000000000000;            }        }    }}int main(){    deal();    int n;    while (cin >> n)    {        int i = 3000;        while (a[n][i] == 0)i--;        printf("%lld", a[n][i] );  //  刚开始不用补0        i--;        for (; i >= 1; i--)            printf("%013lld", a[n][i] );   // 不够13位就补0        printf("\n");    }}
0 0
原创粉丝点击