A - Hex Factorial

A - 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



题意:题目的意思很简单,就是告诉了我们一个数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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值