Painful Bases (状压dp)

As you know that sometimes base conversion is a painful task. But still there are interesting facts in bases.

For convenience let’s assume that we are dealing with the bases from 2 to 16. The valid symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. And you can assume that all the numbers given in this problem are valid. For example 67AB is not a valid number of base 11, since the allowed digits for base 11 are 0 to A.

Now in this problem you are given a base, an integer K and a valid number in the base which contains distinct digits. You have to find the number of permutations of the given number which are divisible by K. K is given in decimal.

For this problem, you can assume that numbers with leading zeroes are allowed. So, 096 is a valid integer. Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. After that there will be two integers, base (2 ≤ base ≤ 16) and K (1 ≤ K ≤ 20). The next line contains a valid integer in that base which contains distinct digits, that means in that number no digit occurs more than once. Output

For each case, print the case number and the desired result. Sample Input

Output for Sample Input

3

2 2

10

10 2

5681

16 1

ABCDEF0123456789

Case 1: 1

Case 2: 12

Case 3: 20922789888000

Problem Setter: Jane Alam Jan

dp[sta][mod]表示当前选数状态为sta,模k为mod时的方案数

/*************************************************************************> File Name: LightOJ1021.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: 2015年06月09日 星期二 19时45分53秒 ************************************************************************/;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;LL;typedef pair <int, int> PLL;LL dp[(1 << 16) + 10][25];char str[20];int trans(char c) {if (c >= ‘0’ && c <= ‘9’) {return c – ‘0’;}return c – ‘A’ + 10;}int main() {int t, icase = 1;scanf(“%d”, &t);while (t–) {int base, k;scanf(“%d%d”, &base, &k);scanf(“%s”, str);int len = strlen(str);for (int i = 0; i < (1 << len); ++i) {for (int j = 0; j < k; ++j) {dp[i][j] = 0;}}dp[0][0] = 1;for (int i = 0; i < (1 << len); ++i) {for (int j = 0; j < len; ++j) {if (i & (1 << j)) {continue;}for (int rest = 0; rest < k; ++rest) {if (!dp[i][rest]) {continue;}dp[i | (1 << j)][(rest * base + trans(str[j])) % k] += dp[i][rest];}}}printf(“Case %d: %lld\n”, icase++, dp[(1 << len) – 1][0]);}return 0;}

,发光并非太阳的专利,你也可以发光

Painful Bases (状压dp)

相关文章:

你感兴趣的文章:

标签云: