forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 11084 - Anagram Division.cpp
69 lines (58 loc) · 1.37 KB
/
UVa 11084 - Anagram Division.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
int N;
int S[10];
int D;
int dp[10][1 << 10][1024];
int search(int idx, int bitMask, int sum)
{
if (idx == N)
return sum % D == 0? 1 : 0;
// Key ideas:
// 1. 123 % 10 = 100 % 10 + 20 % 10 + 3 % 10
// 2. Suppose S is 123456 and we got 10 permutations for
// 123400. Then 432100 has also 10 permutations if
// 432100 % D has the same value with 123400 % D.
if (sum < 1024
&& dp[idx][bitMask][sum] != -1)
return dp[idx][bitMask][sum];
int nPermu = 0;
bool used[10] = {0};
for (int i = 0; i < N; ++i)
{
if (bitMask & (1 << i))
continue;
if (used[S[i]])
continue;
used[S[i]] = true;
nPermu += search(idx + 1,
bitMask | (1 << i),
(sum + (long long)(pow(10.0, idx) + 1e-9) * S[i]) % D);
}
if (sum < 1024)
dp[idx][bitMask][sum] = nPermu;
return nPermu;
}
int solve()
{
memset(dp, -1, sizeof(dp));
return search(0, 0, 0);
}
int main()
{
int T;
cin >> T;
while ( T-- )
{
string s;
cin >> s >> D;
N = (int)s.size();
for (int i = 0; i < s.size(); ++i)
S[i] = s[i] - '0';
cout << solve() << endl;
}
return 0;
}