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

HDU1042(N!:设4为基数)

时间:2016-07-15 23:49:33      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

N!

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


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
 
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
const int MAXN=10005;
const int BASE=100000;
const int LEN=5;//防止int溢出,最多基数最多设为5位
struct BigInt{
    int e[MAXN],len;
    BigInt()
    {
        memset(e,0,sizeof(e));
        len=0;
    }
    void set(int x)
    {
        while(x>0)
        {
            e[len++]=x%BASE;
            x/=BASE;
        }
    }
    BigInt operator*(const BigInt& b)
    {
        BigInt res;
        for(int i=0;i<len;i++)
        {
            int up=0;
            for(int j=0;j<b.len;j++)
            {
                int z=(e[i]*b.e[j]+res.e[i+j]+up);
                res.e[i+j]=z%BASE;
                up=z/BASE;
            }
            if(up!=0)    res.e[i+b.len]=up;
        }
        res.len=len+b.len;
        while(res.len>1&&res.e[res.len-1]==0)    res.len--;
        return res;
    }
    void print()
    {
        cout<<e[len-1];
        for(int i=len-2;i>=0;i--)
        {
            cout<<setw(LEN)<<setfill(0)<<e[i];//输出前导0;
        }
        cout<<endl;
    }
};
int main()
{
    int n;
    while(cin>>n)
    {
        BigInt res;
        res.set(1);
        for(int i=1;i<=n;i++)
        {
            BigInt a;
            a.set(i);
            res=res*a;
        }
        res.print();
    }
    return 0;
}

 

HDU1042(N!:设4为基数)

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/5674618.html

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