[BZOJ]1072
题意:
给一个数字串
s
和正整数
题解:
除了有一个地方没想过去这基本上是道裸题。。
因为
s
的范围很小所以可以状压用过的数字。
整除这种东西不好转移所以用余数为0来表示,
转移的时候考虑在
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
//状压Dp。。不是特别难?
//By sssSSSay
using namespace std;
const int Maxn = 2010;
int T,d,dp[Maxn][Maxn],cnt[Maxn];
char str[Maxn];
int Jc(int x, int temp = 1) {for(int i = 2; i <= x; ++i) temp *= i; return temp;}
int main() {
scanf("%d", &T);
while(T--) {
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
scanf("%s%d", &str, &d);
int s = strlen(str);
for(int i = 0; i < s; ++i) ++cnt[str[i]];
int all = (1 << s) - 1;
dp[0][0] = 1;
for(int i = 0; i <= all; ++i) {
for(int j = 0 ; j < d; ++j) {
if(dp[i][j]) {
for(int k = 0; k < s; ++k) {
if((i & (1 << k))) {
dp[i | (1 << k)][(j * 10 + (str[k] - '0')) % d] += dp[i][j];
}
}
}
}
}
for(int i = 0; i < s; ++i) {
dp[all][0] /= Jc(cnt[str[i]]);
cnt[str[i]] = 0;
}
printf("%d\n",dp[all][0]);
}
return 0;
}