1. 程式人生 > >【BZOJ1072】[SCOI2007]排列perm【暴搜】

【BZOJ1072】[SCOI2007]排列perm【暴搜】

似乎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;
}