码迷,mamicode.com
首页 > 其他好文 > 详细

高精度求N!

时间:2016-05-07 09:06:54      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
const LL MOD = 100000000000000;///省时间
const int MAXN = 40000;
LL a[MAXN];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(a, 0, sizeof(a));
        a[0] = 1;
        int end = 0;
        for(LL i=2; i<=n; i++)
        {
            LL c = 0;///进位
            for(LL j=0; j<=end; j++)///注意这里是个窍门
            {
                LL s = a[j]*i+c;
                a[j] = s%MOD;
                c = s/MOD;
            }
            if(c)
                a[++end] = c;
        }
        printf("%lld",a[end]);
        for(LL i=end-1; i>=0; i--)
        {
            printf("%014I64d",a[i]);
        }
        puts("");
    }
    return 0;
}

高精度求N!

标签:

原文地址:http://blog.csdn.net/qingshui23/article/details/51334625

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!