nyoj--84 阶乘的0

来源:互联网 发布:jquery 2.1.1.min.js 编辑:程序博客网 时间:2024/04/29 10:08

nyoj 84

题解

算术基本定理,每个大于1的正整数可以分解成: n=pa11pa22...pakk,这些素数中,相乘末尾有0的只有25,而一个数的阶乘中2的次幂总是高于5的次幂的,所以只需要知道阶乘中5的次幂即可。

#include <iostream>#include <vector>using namespace std;int main(){    int n, t;    vector<int> fivepow;    int pow = 1;    while(pow <= 10000000)    {        pow *= 5;        fivepow.push_back(pow);    }    for(cin >> t; t--; )    {        cin >> n;        int ans = 0;        /*for(int i = 0; i < fivepow.size(); ++i)        {            ans += n / fivepow[i];        }*/        while(n)        {            ans += n / 5;            n /= 5;        }        cout << ans << endl;    }    return 0;}
0 0