给定两个数n,m,其中m是一个素数。
将n(0<=n<=2^31)的阶乘分解质因数,求其中有多少个m。
注:^为求幂符号。
3 100 5 16 2 1000000000 13
24 15 83333329
分析:
这道题的关键是效率,找出最有效的算法。(和《编程之美》上的某道题类似)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int s,n,m,ans;
cin>>s;
while(s--)
{
cin>>n>>m;
ans=0;
if(n<2)
cout<<ans<<endl;
else
{
while(n/m)
{
ans+=(n/m);
n/=m;
}
}
cout<<ans<<endl;
}
return 0;
}原文地址:http://blog.csdn.net/u011694809/article/details/46505145