The problem statement is very easy. Given a number n you have to determine the largest power of m,
not necessarily prime, that divides n!.
Input
The input file consists of several test cases. The first line in the file is the number of cases to handle.
The following lines are the cases each of which contains two integers m (1 < m < 5000) and n
(0 < n < 10000). The integers are separated by an space. There will be no invalid cases given and
there are not more that 500 test cases.
Output
For each case in the input, print the case number and result in separate lines. The result is either an
integer if m divides n! or a line ‘Impossible to divide’ (without the quotes). Check the sample input
and output format.
我是在其它博客看的题解,非常的机智,很多细节,膜拜:http://blog.csdn.net/u011345136/article/details/38658977?utm_source=tuicool&utm_medium=referral
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define inf 0x3f3f3f3f;
using namespace std;
int main()
{
int N,m,n,num,i,sum,p,ans,x=1;
cin>>N;
while(N--)
{
i=2;
ans=inf;
cin>>m>>n;
while(m!=1)
{
p=0;
while(m%i==0)
{
m=m/i;
p++;
}
if(p)
{
num=n;
sum=0;
while(num)
{
sum+=num/i;
num=num/i;
}
ans = min(ans, sum/p);
}
i++;
}
cout<<"Case "<<x++<<":"<<endl;
if(ans)
cout<<ans<<endl;
else
cout<<"Impossible to divide"<<endl;
}
return 0;
}
“`