-
Notifications
You must be signed in to change notification settings - Fork 12
/
1341-Aladdin_and_the_Flying_Carpet.cpp
81 lines (73 loc) · 1.58 KB
/
1341-Aladdin_and_the_Flying_Carpet.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
77
78
79
80
81
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> Vi;
typedef pair<ll, int> P;
typedef vector<P> Vp;
const int MAX_PRIME = 1000000;
Vi primes;
void compute_primes() {
Vi erat(MAX_PRIME + 1, 1);
for (int i = 2; i*i <= MAX_PRIME; ++i) {
if (erat[i]) {
for (int j = i*i; j <= MAX_PRIME; j += i) {
erat[j] = 0;
}
}
}
for (int i = 2; i <= MAX_PRIME; ++i) {
if (erat[i]) {
primes.push_back(i);
}
}
}
ll sq(ll n) {
return n*n;
}
Vp factor(ll n) {
Vp res;
for (int i = 0; i < int(primes.size()) && sq(primes[i]) <= n; ++i) {
int f = 0;
while (n%primes[i] == 0) {
n /= primes[i];
++f;
}
if (f != 0) {
res.push_back(P(primes[i], f));
}
}
if (n != 1) {
res.push_back(P(n, 1));
}
return res;
}
int bt(int i, const Vp& v, ll t, ll a, ll b) {
if (i == int(v.size())) {
return t >= b ? 1 : 0;
}
int res = 0;
for (int j = 0; j <= v[i].second; ++j) {
if (t > MAX_PRIME || t*t >= a) {
break;
}
res += bt(i + 1, v, t, a, b);
t *= v[i].first;
}
return res;
}
int main() {
compute_primes();
int tcas;
cin >> tcas;
for (int cas = 1; cas <= tcas; ++cas) {
ll a, b;
cin >> a >> b;
cout << "Case " << cas << ": ";
if (a == 1) {
cout << 0 << endl;
} else {
cout << bt(0, factor(a), 1, a, b) << endl;
}
}
}