-
Notifications
You must be signed in to change notification settings - Fork 96
/
6.8.cpp
76 lines (70 loc) · 1.9 KB
/
6.8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* 题目名称:整除问题
* 题目来源:上海交通大学复试上机题
* 题目链接:http://t.cn/Aip7eHBD
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <climits>
using namespace std;
const int MAXN = 1e3 + 10;
const int INF = INT_MAX;
vector<int> prime;
bool isPrime[MAXN];
void Initial() {
for (int i = 0; i < MAXN; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i < MAXN; ++i) {
if (!isPrime[i]) {
continue;
}
prime.push_back(i);
if (i > MAXN / i) {
continue;
}
for (int j = i * i; j < MAXN; j += i) {
isPrime[j] = false;
}
}
return ;
}
int main() {
Initial();
int n, a;
while (scanf("%d%d", &n, &a) != EOF) {
map<int, int> exponentN; //存储n!的质因数分解
for (int j = 2; j <= n; ++j) {
int number = j;
for (int i = 0; i < prime.size() && prime[i] <= number; ++i) {
while (number % prime[i] == 0) {
exponentN[prime[i]]++;
number /= prime[i];
}
}
}
map<int, int> exponentA; //存储a的质因数分解
for (int i = 0; i < prime.size() && prime[i] <= a; ++i) {
while (a % prime[i] == 0) {
exponentA[prime[i]]++;
a /= prime[i];
}
}
int answer = INF;
for (map<int, int>::iterator it = exponentA.begin(); it != exponentA.end(); ++it) {
if (exponentN.find(it->first) == exponentN.end()) {
answer = 0;
} else {
answer = min(answer, exponentN[it->first] / it->second);
}
}
printf("%d\n", answer);
}
return 0;
}