题意:求最大的正整数k使得m^k|n!.
把k分解,然后1~n每一个都分解,判断一下k分解后的的每部分都最多能够取
多少次就行了.
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> using namespace std; #define maxn 11111 bool is_prime[maxn]; int n, m; int prime[maxn], cnt; void init () { cnt = 0; memset (is_prime, 1, sizeof is_prime); is_prime[0] = is_prime[1] = 0; for (int i = 2; i < maxn; i++) { if (is_prime[i]) { prime[cnt++] = i; for (int j = i+i; j < maxn; j += i) is_prime[j] = 0; } } } int fac[maxn], facc[maxn]; void fenjie (int m) { memset (fac, 0, sizeof fac); for (int i = 0; i < cnt; i++) { while (m%prime[i] == 0) { m /= prime[i]; fac[prime[i]]++; } } } void solve () { fenjie (m); memset (facc, 0, sizeof facc); for (int i = 2; i <= n; i++) { int num = i; for (int j = 0; prime[j]*prime[j] <= num; j++) { if (num%prime[j] == 0) { while (num%prime[j] == 0) { num /= prime[j]; facc[prime[j]]++; } } } if (num > 1) facc[num]++; } int ans = 100000000; for (int i = 0; i < cnt; i++) { if (fac[prime[i]]) { if (facc[prime[i]] >= fac[prime[i]]) { ans = min (ans, facc[prime[i]]/fac[prime[i]]); } else { printf ("Impossible to divide\n"); return ; } } } printf ("%d\n", ans); } int main () { init (); int t, kase = 0; scanf ("%d", &t); while (t--) { printf ("Case %d:\n", ++kase); scanf ("%d%d", &m, &n); solve (); } return 0; }