似乎std是状压?
算了下O(Tn!)的确可以过,敲了一发,真过了...
/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ULL;
typedef unsigned int uint;
const int maxn = 15;
int n, num[maxn];
uint d;
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
char str[maxn];
inline ULL getnum() {
ULL ans = 0;
for(int i = 1; i <= n; i++) {
ans *= 10;
ans += num[i];
}
return ans;
}
int main() {
for(int T = iread(); T; T--) {
scanf("%s", str + 1); n = strlen(str + 1); d = iread();
for(int i = 1; i <= n; i++) num[i] = str[i] - '0';
sort(num + 1, num + 1 + n);
int ans = 0;
do {
ULL x = getnum();
if(x % d == 0) ans++;
} while(next_permutation(num + 1, num + 1 + n));
printf("%d\n", ans);
}
return 0;
}