#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
const int M = 50010;
long long dp[(1<<16)+10][30];
char s[20];
int change(char x){
if(x>='0' && x<='9')
return x-'0';
return x-'A'+10;
}
int main()
{
int T,cas=1;
int base, mod;
scanf("%d",&T);
while(T--){
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
scanf("%d%d%s",&base,&mod,s);
int len = strlen(s);
for(int i=1; i<1<<len; i++){
for(int j=i; j>0; j-=(j&-j)){
int aim = i&~(j&-j);
int tmp = j&-j;
int id = 0;
while(tmp!=(1<<id)) id++;
for(int k=0; k<mod; k++)
dp[i][(k*base+change(s[id]))%mod] += dp[aim][k];
}
}
printf("Case %d: %lld\n",cas++,dp[(1<<len)-1][0]);
}
return 0;
}
【状压dp】Light oj 1021 Painful Bases
最新推荐文章于 2024-03-06 22:08:55 发布