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
题目大意:
求 在16进制下,有多少位是0。
分析:
由于N的范围较小,可以直接使用高精度整数的乘法算出结果,再计算0的个数。注意乘法过程中直接使用16进制计算。
具体解释见代码。
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int mmax = 10010;
const int inf = 0x3fffffff;
struct Bignum
{
int Sz;
int num[mmax];//根据实际情况确定数组长度
void print()
{
for(int i=Sz-1;i>=0;i--)
printf("%0x",num[i]);
puts("");
}
Bignum() {}
Bignum(int sz,char *a)
{
Sz=sz;
for(int i=0;i<Sz;i++)
{
num[i]=a[Sz-1-i]-'0';
}
}
Bignum(int sz,int *a)
{
Sz=sz;
for(int i=0;i<Sz;i++)
num[i]=a[i];
}
//以16进制存储
Bignum(int x)
{
Sz=0;
while(x)
{
num[Sz++]=x%16;
x/=16;
}
}
Bignum operator + (const Bignum &a)
{
int tmp[mmax];
memset(tmp,0,sizeof tmp);
int len=max(Sz,a.Sz);
for(int i=0;i<len;i++)
{
tmp[i]+=num[i]+a.num[i];
tmp[i+1]+=tmp[i]/10;
tmp[i]%=10;
}
return Bignum(len+(tmp[len]?1:0),tmp);
}
Bignum operator - (const Bignum &a)
{
int tmp[mmax];
memset(tmp,0,sizeof tmp);
int len=max(Sz,a.Sz);
for(int i=0;i<len;i++)
{
tmp[i]+=num[i]-a.num[i];
if(tmp[i]<0)
{
tmp[i+1]-=1;
tmp[i]+=10;
}
}
for(int i=len-1;i>=0;i--)
{
if(tmp[i])
return Bignum(i+1,tmp);
}
return Bignum(1,tmp);
}
//16进制大数乘法
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]/16;
tmp[i]%=16;
}
for(int i=Sz+a.Sz-1;i>=0;i--)
{
if(tmp[i])
return Bignum(i+1,tmp);
}
return Bignum(1,tmp);
}
};
int main()
{
int n;
while(~scanf("%d",&n) && n>=0)
{
Bignum ans(1);
for(int i=1;i<=n;i++)
ans=ans*Bignum(i);
int cnt=0;
for(int i=0;i<ans.Sz;i++)
{
if(ans.num[i]==0)
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}