基德KID.1412 发表于 2013-1-26 12:36:56

【奇妙的数论】HDU 1124 Factorial

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

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

Sample Input
6    //表示数据个数
3
60
100
1024
23456
8735373

Sample Output
0
14
24
253
5861
2183837

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个


#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;}
页: [1]
查看完整版本: 【奇妙的数论】HDU 1124 Factorial