forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3662 - Math Magic.cpp
102 lines (77 loc) · 2 KB
/
3662 - Math Magic.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MOD 1000000007
vector<int> p,e;
int np;
int get_mask(int x){
int ret = 0;
for(int i = 0;i < np;++i){
int aux = 0;
while(x % p[i] == 0){
x /= p[i];
++aux;
}
if(aux == e[i]) ret |= (1 << i);
}
return ret;
}
int memo[100][1001][1 << 5];
vector<int> div,use;
int ndiv;
int solve(int pos, int sum, int mask){
if(sum < 0) return 0;
if(pos == -1){
if(sum == 0 && mask == (1 << np) - 1) return 1;
return 0;
}
int &ret = memo[pos][sum][mask];
if(ret == -1){
ret = 0;
for(int i = 0;i < ndiv;++i){
ret += solve(pos - 1,sum - div[i],mask | use[i]);
if(ret >= MOD) ret -= MOD;
}
}
return ret;
}
int main(){
int N,M,K;
while(scanf("%d %d %d",&N,&M,&K) == 3){
p.clear(); e.clear(); np = 0;
int M2 = M;
for(int i = 2;i <= M2 / i;++i){
if(M2 % i == 0){
p.push_back(i); ++np;
int aux = 0;
while(M2 % i == 0){
M2 /= i;
++aux;
}
e.push_back(aux);
}
}
if(M2 != 1){
p.push_back(M2);
e.push_back(1);
++np;
}
div.clear(); use.clear(); ndiv = 0;
for(int i = 1;i <= M / i;++i){
if(M % i == 0){
div.push_back(i);
use.push_back(get_mask(i));
++ndiv;
if(i != M / i){
div.push_back(M / i);
use.push_back(get_mask(M / i));
++ndiv;
}
}
}
memset(memo,-1,sizeof memo);
printf("%d\n",solve(K - 1,N,0));
}
return 0;
}