-
Notifications
You must be signed in to change notification settings - Fork 34
/
1021-Painful-Bases.cpp
96 lines (72 loc) · 1.4 KB
/
1021-Painful-Bases.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define unset(x, i) (x & ~(1 << i))
#define set(x, i) (x | ((1 << i)))
#define check(x, i) (x & (1 << i))
using namespace std;
int base;
int by;
int n;
int a[30];
int rem[25];
int l;
unsigned long long dp[1 << 16][20];
unsigned long long pow[20];
bool vis[1 << 16];
unsigned long long explore(unsigned long long mask)
{
if(vis[mask] == 1 or mask == 0) {
return 0;
}
vis[mask] = 1;
for (int i = 0; i < by; i++)
dp[mask][i] = 0;
for (int i = 0; i < base; i++) {
if(check(mask, i)) {
l--;
explore(unset(mask, i));
l++;
for (int j = 0; j < by; j++) {
dp[mask][(pow[l - 1] * i + j) % by] += dp[unset(mask, i)][j];
}
}
}
}
int main()
{
int t;
char c;
char temp[20];
unsigned long long mask;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &base);
scanf("%d", &by);
scanf("%s", temp);
n = strlen(temp);
l = n;
pow[0] = 1;
mask = 0;
memset(dp, 0, sizeof dp);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) {
pow[i] = pow[i-1] * base;
pow[i] = pow[i] % by;
}
for (int i = 0; i < n; i++) {
if(isalpha(temp[i])) {
a[i] = (temp[i] - 'A') + 10;
}
else {
a[i] = (temp[i] - '0');
}
mask = set(mask, a[i]);
}
dp[0][0] = 1;
explore(mask);
printf("Case %d: %llu\n", cs, dp[mask][0]);
}
}