HDU 2940 Hex Factorial 高精度乘法

来源:互联网 发布:网络攻击与防范 编辑:程序博客网 时间:2024/06/15 05:28
                       Hex Factorial

Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
The expression N!, reads as the factorial of N, denoting the product of the first N positive integers. If the factorial of N is written in hexadecimal without leading zeros, can you tell us how many zeros are there in it? Take 15! as an example, you should answer “3” because (15)10! = (13077775800)16, and there are 3 zeros in it.
Input
The input contains several cases. Each case has one line containing a non-negative decimal integer N (N ≤ 100). You need to count the zeros in N! in hexadecimal. A negative number terminates the input.
Output
For each non-negative integer N, output one line containing exactly one integer, indicating the number of
zeros in N!.
Sample Input
1
15
-1
Sample Output
0
3

题意:给你一个10进制数n  计算n的阶乘转化为16进制后中有多少个0
思路:先吧n转化为16进制 然后按照16进制的进位进行高精度乘法
ACcode:

#include<bits/stdc++.h>using namespace std;#define mmax  10010#define new 16struct Bignum{    int Sz;    int num[mmax];    Bignum(int sz,int *a)    {        Sz=sz;        for(int i=0;i<Sz;i++)            num[i]=a[i];    }    Bignum(int x)    {        Sz=0;        while(x)        {            num[Sz++]=x%new;            x/=new;        }    }    Bignum operator * (const Bignum &a)    {        int tmp[mmax];        memset(tmp,0,sizeof tmp);        for(int i=0;i<Sz;i++)          for(int j=0;j<a.Sz;j++)            {                tmp[i+j]+=num[i]*a.num[j];            }        for(int i=0;i<Sz+a.Sz;i++)        {            tmp[i+1]+=tmp[i]/new;            tmp[i]%=new;        }        for(int i=Sz+a.Sz-1;i>=0;i--)        {            if(tmp[i])                return Bignum(i+1,tmp);        }        return Bignum(1,tmp);    }};int num[110];int main(){     Bignum ans(1);    for(int i=1;i<=100;i++)    {            ans=ans*Bignum(i);        int cnt=0;        for(int i=0;i<ans.Sz;i++)        {            if(ans.num[i]==0)                  cnt++;        }        num[i]=cnt;    }    int n;    while(scanf("%d",&n)!=EOF)    {       if( n < 0)  break;       printf("%d\n",num[n]);    }    return 0;}
0 0
原创粉丝点击