很好的一道题。由于n最大1000,1000的阶乘过于大,故直接计算出来再判断整除次数肯定是不行。
于是就想到了把n和a都分解质因数,幂次分别保留在不同数组中,针对a的质因数找出n和a这些质因数的幂次,看整体上n的幂次为a的幂次的多少倍。具体来说这些质因数中n的幂次都是a的幂次的倍数,找出最小的倍数即可满足。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cctype>
#include <cmath>
#include <climits>
using namespace std;
const int MAXN = 1005;
const int INF = INT_MAX;
bool isPrime[MAXN];
vector<int> prime;
int prime1num[MAXN];
int prime2num[MAXN];
void Initial(){
for(int i = 2; i < MAXN; i++){
isPrime[i] = true;
}
for(int i = 2; i < MAXN; i++){
if(!isPrime[i]) continue;
prime.push_back(i);
for(int j = i*i; j < MAXN; j += i){
if(isPrime[j]) isPrime[j] = false;
}
}
}
int main(){
// freopen("in.txt", "r", stdin);
Initial();
int n, a, k, factor;
while(~scanf("%d %d", &n, &a)){
memset(prime1num, 0, sizeof(prime1num));
memset(prime2num, 0, sizeof(prime2num));
while(n != 1){
int tmp = n;
for(int i = 0; i < prime.size() && prime[i] <= tmp; i++){
factor = prime[i];
while(tmp % factor == 0){
prime1num[prime[i]]++;
tmp /= factor;
}
}
n--;
}
vector<int> target;
for(int i = 0; i < prime.size() && prime[i] <= a; i++){
factor = prime[i];
bool flag = false;
while(a % factor == 0){
flag = true;
prime2num[prime[i]]++;
a /= factor;
}
if(flag) target.push_back(prime[i]);
}
int ans = INF;
for(int i = 0; i < target.size(); i++){
ans = min(ans, prime1num[target[i]]/prime2num[target[i]]);
//printf("%d : %d %d\n", target[i], prime1num[target[i]], prime2num[target[i]]);
}
printf("%d\n", ans);
}
return 0;
}
来源:CSDN
作者:Flynn_curry
链接:https://blog.csdn.net/Flynn_curry/article/details/104747574