计算数 m 和数 2 ~n 的 GCD,若 GCD > 1,则 m /= GCD,若最后 m 不为 1 则表明 m 不能整除 n!,
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M 1000005
typedef long long ll;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
bool isprime(int x)
{
double tx=sqrt(1.0*x);
int it=2;
while(it<=tx&&x%it!=0)
it++;
if(it>tx)
return true;
else
return false;
}
/*bool isprime(int number)
{
if (number == 2)
return true;
if (number % 2 == 0)
return false;
for (int c = 3; c <= ceil(sqrt(1.0*number)); c += 2)
if (number % c == 0)
return false;
return true;
}*/
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0)
{
printf("%d does not divide %d!\n",m,n);
continue;
}
if(m==1)
{
printf("%d divides %d!\n",m,n);
continue;
}
if(isprime(m)&&m>n)
{
printf("%d does not divide %d!\n",m,n);
continue;
}
if(m<=n)
{
printf("%d divides %d!\n",m,n);
continue;
}
bool flag=false;
int tm=m;
int gc;
for(int i=n;i>=2;i--)
{
gc=gcd(tm,i);
if(gc>1)
{
tm=tm/gc;
if(tm<i)
{
flag=true;
break;
}
if(isprime(tm))
{
break;
}
}
}
if(flag)
printf("%d divides %d!\n",m,n);
else
printf("%d does not divide %d!\n",m,n);
}
return 0;
}