1021-painful Bases

Source: Internet
Author: User

1021-painful Bases
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

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

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

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

For the problem, you can assume that numbers with leading zeroes is 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 would be the integers, base (2≤base≤16) and K (1≤k≤20). The next line contains a valid integer in that base which contains distinct digits, which means in that number no digit OCC URS 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:20,922,789,888,000

Test instructions: give you the n for the binary, the back of the M is modulus, and then give you a string of numbers for the current number of numbers, ask you to split this number, and then the whole arrangement of the new number, ask how many of these numbers is the multiple of M;

IDEA: State compression DP;

DP[I][J] means the number of M modulus to J in the I state; we can combine these numbers, then the number is 2n, then each combination is a state, then the possible modulus in each state is m-1, then I realize the whole arrangement,

The whole arrangement is the combined number of n! Then each state can be pushed from the previous state so this is the whole arrangement, but the same merge.

The state transition equation looks at the following code:

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6#include <math.h>7#include <queue>8#include <stack>9#include <vector>Ten using namespacestd; OnetypedefLong LongLL; A Charans[ -]; - intshu[ -]; -LL dp[1<< -+1][ A]; the intMainvoid) - { -         inti,j,k; -scanf"%d",&k); +         ints; -          for(s=1; s<=k; s++) +         { A                 intn,m; atscanf"%d%d",&n,&m); -scanf"%s", ans); -                 intL=strlen (ans); -                  for(i=0; i<l; i++) -                 { -                         if(ans[i]>='0'&&ans[i]<='9') in                         { -shu[i]=ans[i]-'0'; to                         } +                         Else -                         { theshu[i]=ans[i]-'A'+Ten; *                         } $                 }Panax NotoginsengMemset (DP,0,sizeof(DP)); -dp[0][0]=1; the                  for(i=0; i< (1&LT;&LT;L); i++) +                 { A                          for(j=0; j<=m-1; J + +) the                         { +                                  for(ints=0; s<l; s++) -                                 { $                                         intak=J; $                                         if(! (i& (1<<s ))) -                                         { -Dp[i| (1<<s] [(Ak*n+shu[s])%m]+=Dp[i][j]; the                                         } -                                 }Wuyi                         } the                 } -printf"Case %d:%lld\n", s,dp[(1&LT;&LT;L)-1][0]); Wu         } -         return 0; About}

1021-painful Bases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.