1057 N的阶乘 【数论】

来源:互联网 发布:雪姨王琳撕网络喷子 编辑:程序博客网 时间:2024/06/05 15:11
1057 N的阶乘
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 收藏
 关注
输入N求N的阶乘的准确值。
 
Input
输入N(1 <= N <= 10000)
Output
输出N的阶乘
Input示例
5
Output示例
120模拟平时int类型对10取余进位,输出还需要注意格式,%013lld是不足13位左补0.
#include<stdio.h>#define mod 10000000000000#define N 1000010#define LL long longLL num[N];int main(){    LL i,j,n;    LL k,ans,t;    while(scanf("%lld",&n)!=EOF)    {        ans = 1;        num[1] = 1;        for(i = 1; i <= n; i ++)        {            k = 0;            for(j = 1; j <= ans; j ++)            {                t = num[j]*i + k;                num[j] = t%mod;                k = t/mod;//k保存进位数             }            if(k)//如果最后还需要进位                 num[++ans] = k;        }        printf("%lld",num[ans]);        for(i = ans-1; i >= 1; i --)            printf("%013lld",num[i]);        printf("\n");    }    return 0;}

原创粉丝点击