-
Notifications
You must be signed in to change notification settings - Fork 1
/
1021 - Painful Bases.cpp
55 lines (55 loc) · 1.16 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int setBit(int N, int pos)
{
return (N|(1<<pos));
}
bool isNotSet(int N, int pos)
{
return !(N&(1<<pos));
}
ll dp[20][(1<<16)+2];
int k,n,base;
int arr[17];
char str[17];
int getMod(int md, int num)
{
md = ((md*base) + num)%k;
return md;
}
ll fun(int mask, int mod)
{
if(mask==(1<<n)-1){
return (mod==0) ? 1 : 0;
}
if(dp[mod][mask]!=-1) return dp[mod][mask];
ll ret = 0;
for(int i=0; i<n; i++){
if(isNotSet(mask,i)){
int md = getMod(mod, arr[i]);
ret+=fun(setBit(mask,i),md);
}
}
return dp[mod][mask] = ret;
}
int main()
{
map<char, int> mp;
int tc,cs=1;
tc=0;
for(char i='0'; i<='9'; i++) mp[i] = tc++;
for(char i='A'; i<='F'; i++) mp[i] = tc++;
scanf("%d",&tc);
while(tc--){
scanf("%d%d",&base, &k);
scanf("%s",str);
n = strlen(str);
for(int i=0; i<n; i++){
arr[i] = mp[ str[i] ];
}
memset(dp, -1, sizeof dp);
printf("Case %d: %lld\n",cs++, fun(0,0));
}
return 0;
}