pku 1401 Factorial 算数基本定理 && 51nod 1003 阶乘后面0的数量

链接:http://poj.org/problem?id=1401

题意:计算N!的末尾0的个数

思路:算数基本定理

有0,分解为2*5,寻找2*5的对数,2的因子个数大于5,转化为寻找因子5的个数。又有算数基本定理:

n!在素数因子分解中p的幂为[n/p]+[n/p2]+[n/p3]+...

同时最大次数不会超过logpn。通过换底公式,有ln(n)/ln(p)

代码:(51Nod去掉t循环即可)

 1 #include <iostream>
 2 #include <math.h>
 3 using namespace std;
 4 int main() {
 5     //freopen("in.txt","r",stdin);
 6     //freopen("out.txt","w",stdout);
 7     ios::sync_with_stdio(false);
 8     int n,t;
 9     cin>>t;
10     while(t--) {
11         cin>>n;
12         int num=5,sum=0,index=(int)(log(n*1.0)/log(5*1.0));
13         for(int i=1; i<=index; ++i) {
14             sum+=n/num;
15             num*=5;
16         }
17         cout<<sum<<endl;
18     }
19     return 0;
20 }
View Code

 

posted @ 2017-11-16 17:50  lemonsbiscuit  阅读(204)  评论(0编辑  收藏  举报