A - Hex Factorial

来源:互联网 发布:淘宝店铺货源 编辑:程序博客网 时间:2024/06/06 03:37

A - Hex Factorial
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

115-1
 

Sample Output

03



题意:题目的意思很简单,就是告诉了我们一个数n,然后让我们求n!的十六进制形式中有多少个0,输入以一个负数结束。


因为N的极限为100,所以要使用高精度,直接暴力跑一遍对于每一个输入找到一个答案然后O(1)询问即可,在转换为16进制的时候一位一位的转换即可。


#include"iostream"#include"cstring"#include"cstdio"using namespace std;int ans[105];int num[2005];void getans(){    memset(ans,0,sizeof(ans));    memset(num,0,sizeof(num));    num[0] = 1;    for(int i = 2;i <= 100;i++)    {        //cout << "***" << endl;        int jinwei = 0;        for(int j = 0;j < 2005;j++)        {            int greatnum = num[j]*i + jinwei;            num[j] = greatnum % 16;            jinwei = greatnum / 16;        }        int pos;        for(pos = 2004;pos >= 0;pos--)            if(num[pos])                 break;        for(int zz = 0;zz < pos;zz ++)             if(!num[zz])                 ans[i]++;    }}int main(void){    int n;    getans();    while(~scanf("%d",&n))    {        if(n < 0) break;        printf("%d\n",ans[n]);    }    return 0;}


0 0
原创粉丝点击