题意:给定m和n,求出最大的k,使得m^k能整除n的阶乘。
思路:各自分解质因数,然后对比下就能找出k值。
# include <iostream> # include <cstdio> # include <cstring> using namespace std; const int MAXN = 10000; int a[MAXN+1]={0}, b[MAXN+1], c[MAXN][2], d[200][2], k=0; void init() { for(int i=2; i<=MAXN; ++i) if(!a[i]) { b[k++] = i; for(int j=i; j<=MAXN; j+=i) a[j] = 1; } } void fun1(int n, int &cnt)//分解n { int tmp = n; for(int i=0; i<k; ++i) { if(n >= b[i]) { c[cnt][0] = b[i]; while(tmp) { c[cnt][1] += tmp/b[i]; tmp /= b[i]; } tmp = n; ++cnt; } else break; } if(!a[n]) { c[cnt][0] = n; c[cnt][1] = 1; ++cnt; } } void fun2(int n, int &cnt)//分解m { for(int i=0; i<k && b[i]*b[i]<=n; ++i) { if(n % b[i] == 0) { d[cnt][0] = b[i]; for(;n%b[i]==0;d[cnt][1]++,n/=b[i]); ++cnt; } } if(n != 1) { d[cnt][0] = n; d[cnt][1] = 1; ++cnt; } } int main() { int t, m, n, cnt1, cnt2, ans,cas=1; init(); scanf("%d",&t); while(t--) { cnt1 = cnt2 = 0; ans = 0x3f3f3f3f; bool flag = true; memset(c, 0, sizeof(c)); memset(d, 0, sizeof(d)); scanf("%d%d",&m,&n); printf("Case %d:\n",cas++); fun1(n, cnt1); fun2(m, cnt2); for(int i=0; i<cnt2; ++i) { bool ok = false; for(int j=0; j<cnt1; ++j) { if(c[j][0] == d[i][0]) { if(c[j][1] >= d[i][1]) { ok = true; ans = min(ans, c[j][1]/d[i][1]); } else break; } } if(!ok) { flag = false; break; } } if(!flag) puts("Impossible to divide"); else printf("%d\n",ans); } return 0; }