1021 - Painful Bases
Time Limit: 2 second(s) Memory Limit: 32 MB

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

题意:给你的n为进制,后面的m为模数,然后给你一串数字为当前进制下的数,问你拆分这个数,然后再全排列组成新的数,问这些数中有多少是m的倍数;

思路:状态压缩dp;

dp[i][j]表示在i状态下对m取模为j的种数;我们可以将这些数组合,那么种数就是2n,然后每一种组合就是一种状态,那么每种状态下可能的模数有m-1种,那么咋实现全排列,

全排列就是组合数承n!;那么每种状态可以由前面的状态推来那么这就是全排列的过程,只不过将相同的合并。

状态转移方程看下面代码:

 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>
10 using namespace std;
11 typedef long long LL;
12 char ans[100];
13 int shu[100];
14 LL dp[1<<16+1][22];
15 int main(void)
16 {
17 int i,j,k;
18 scanf("%d",&k);
19 int s;
20 for(s=1; s<=k; s++)
21 {
22 int n,m;
23 scanf("%d %d",&n,&m);
24 scanf("%s",ans);
25 int l=strlen(ans);
26 for(i=0; i<l; i++)
27 {
28 if(ans[i]>='0'&&ans[i]<='9')
29 {
30 shu[i]=ans[i]-'0';
31 }
32 else
33 {
34 shu[i]=ans[i]-'A'+10;
35 }
36 }
37 memset(dp,0,sizeof(dp));
38 dp[0][0]=1;
39 for(i=0; i<(1<<l); i++)
40 {
41 for(j=0; j<=m-1; j++)
42 {
43 for(int s=0; s<l; s++)
44 {
45 int ak=j;
46 if(!(i&(1<<s)))
47 {
48 dp[i|(1<<s)][(ak*n+shu[s])%m]+=dp[i][j];
49 }
50 }
51 }
52 }
53 printf("Case %d: %lld\n",s,dp[(1<<l)-1][0]);
54 }
55 return 0;
56 }

最新文章

  1. 十进制(decimal system)转换函数说明
  2. overflow:hidden清楚浮动的影响
  3. 算法与数据结构之顺序查找(C语言)
  4. Qt4.8.6 Embedded Linux 的编译与移植
  5. 剑指架构师系列-InnoDB存储引擎、Spring事务与缓存
  6. UML从需求到实现---类图(1)
  7. 设计模式:命令模式(Command)
  8. selenium 加载RemoteDriver浏览器驱动
  9. C#操作XML的完整例子——XmlDocument篇
  10. 论文阅读(2014-2)----The YouTube Video Recommendation System
  11. 我是如何开发一个连锁企业的信息系统的,NO.1
  12. hibernate HQL查询 2.2
  13. ORACLE SEQUENCE的简单介绍
  14. Java数据结构和算法(七)——链表
  15. BringWindowToTop完美激活窗口与置顶
  16. Python笔记(十五):匿名函数和@property
  17. 谁说前端不需要懂-Nginx反向代理与负载均衡
  18. mysql数据库介绍
  19. Reaction 开源可自定义实时的电商平台
  20. type使用细则

热门文章

  1. 学习java的第二十七天
  2. Shell $()、${}、$[]、$(())
  3. day04:Python学习笔记
  4. Learning Spark中文版--第四章--使用键值对(1)
  5. 乱序拼图验证的识别并还原-puzzle-captcha
  6. vim编码设置(转)
  7. jvm的优化
  8. 深入理解java动态代理机制
  9. shell脚本 检查mysql节点数据一致性
  10. 「Spark从精通到重新入门(二)」Spark中不可不知的动态资源分配