加入收藏 | 设为首页 | 会员中心 | 我要投稿 厦门网 (https://www.xiamenwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数阶乘(1000级别) 51Nod 1057

发布时间:2021-05-27 07:43:38 所属栏目:大数据 来源:网络整理
导读:题目链接: 51Nod 1057 问题描述 输入N求N的阶乘的准确值。 Input 输入N(1 = N = 10000) Output 输出N的阶乘 Input示例 5 Output示例 120 思路: 一百万亿进制(14个0),大数相乘,求阶乘 代码: #includeiostream #includestdio.h #includealgorithm #define

题目链接:

51Nod 1057

问题描述

输入N求N的阶乘的准确值。

Input

输入N(1 <= N <= 10000)

Output

输出N的阶乘

Input示例

5

Output示例

120

思路:

一百万亿进制(14个0),大数相乘,求阶乘

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
#define mod 100000000000000;//14个0
const int N = 1000086;
LL a[N]; 

int main()
{
    int n;
    scanf("%d",&n);
    LL l = 0;
    a[0] = 1;

    for(int i = 1; i <= n; i++)
    {
        LL c = 0;
        for(int j = 0; j <= l; j++)
        {
            LL t = a[j] * i + c;
            a[j] = t % mod;
            c = t / mod;
        }
        if(c != 0)
        {
            l++;
            a[l] = c;
        }
    }
    printf("%lld",a[l]);

    for(int i = l-1; i >= 0; i--)
    {
        printf("%014lld",a[i]);//保留前导0
    }
    return 0;
}

(编辑:厦门网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读