HDU1042(N!:设4为基数)

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;
}

 

posted on 2016-07-15 19:22  vCoders  阅读(145)  评论(0编辑  收藏  举报

导航