高精度求N!

来源:互联网 发布:苏联伪军数量 知乎 编辑:程序博客网 时间:2024/04/30 05:02
#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;}
0 0
原创粉丝点击