【奇妙的数论】HDU 1124 Factorial

[url]http://acm.hdu.edu.cn/showproblem.php?pid=1124[/url]

[size=medium]题意:N阶乘有多少个尾0?(1 <= N <= 1000000000)

[color=red]Sample Input
6[/color] //表示数据个数[color=blue]
3
60
100
1024
23456
8735373[/color]

[color=red]Sample Output[/color][color=blue]
0
14
24
253
5861
2183837[/color]

N! = 1 * 2 * 3 * (2*2) * 5 * (2*3) * 7...

产生10的原因是有2,5的因子,显然在N!中2的个数大于5的个数,所以只需求出5的个数即可

求 N! (1*2*3*4*5*...*N)里有多少个5其实可以转化成:
N!中:是5的倍数的数+是5^2的倍数的数+5^3.....

如50!:
含有10个5的倍数的数:5,15,20,25,30,35,40,45,50 【50/5=10】
含有2个5^2的倍数的数:25,50【50/(5^2)=2】
可见N!中一共有12个5相乘,那么尾0也必有12个
[/size]


#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <iomanip>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

int main()
{
int t, res, n;
scanf ("%d", &t);
while (t--)
{
res = 0;
scanf ("%d", &n);
while (n)
{
res += n / 5;
n /= 5;
}
printf ("%d\n", res);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值