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 }

1021 - Painful Bases的更多相关文章

  1. lightoj 1021 - Painful Bases 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1021 #include<cstring> #include<cstd ...

  2. lightoj 1021 - Painful Bases(数位dp+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...

  3. Light OJ 1021 - Painful Bases(状态压缩DP)

    题目大意: 给你一个base 进制的数字,把这个数字的每一位进行全排列,问有多少个数字是可以整除k的. 题目解析: #include<cstdio> #include<cstring ...

  4. Light oj 1021 - Painful Bases

    题意:  给一个B进制的数,一个10进制的数K,B进制数有x位, 对着x位进行全排列的话,有x!种可能, 问这x!的可能中,有多少种可以整除K,各个位置上的数字都不同. 思路:状态压缩,数位DP #i ...

  5. Painful Bases LightOJ - 1021

    Painful Bases LightOJ - 1021 题意:给出0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F中的一些字符(不重复)还有一个进制base,求这些字符的排列形成的ba ...

  6. K - Painful Bases 状压dp

    Painful Bases LightOJ - 1021 这个题目一开始看,感觉有点像数位dp,但是因为是最多有16进制,因为限制了每一个数字都不同最多就有16个数. 所以可以用状压dp,看网上题解是 ...

  7. LightOJ1021 Painful Bases(状压DP)

    容易想到状态dp[n][S][m](S是数字出现的集合),表示前n位用了数字集S且模k余数是m的方案数. 利用 (xy)base % k = ( x*base+y ) % k = (( x%k ) * ...

  8. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  9. dp百题大过关(第一场)

    好吧,这名字真是让我想起了某段被某教科书支配的历史.....各种DP题层出不穷,不过终于做完了orz 虽然各种手糊加乱搞,但还是要总结一下. T1 Monkey Banana Problem    这 ...

随机推荐

  1. 修复UE4编辑器,ClearLog操作导致的崩溃

    UE4 4.24.3版本,编辑器Output Log窗口中,右键--Clear Log操作很大概率会导致编辑器奔溃:解决办法: 相关文件: Engine\Source\Developer\Output ...

  2. Hadoop RPC通信

    Remote Procedure Call(简称RPC):远程过程调用协议 1. 通过网络从远程计算机程序上请求服务 2. 不需要了解底层网络技术的协议(假定某些传输协议的存在,如TCP或UDP) 3 ...

  3. 商业爬虫学习笔记day5

    一. 发送post请求 import requests url = "" # 发送post请求 data = { } response = requests.post(url, d ...

  4. Sibel Tools和Siebel Cilent的安装步骤

    关于Siebel的资料在网上是少之又少,当时安装开发工具的时候花了挺长时间的,把步骤记录了下来. 一安装win32_11gR2_client 首先要安装Oracle数据库的客户端,必须是32位,安装过 ...

  5. String.split()与StringUtils.split()的区别

    import com.sun.deploy.util.StringUtils; String s =",1,,2,3,4,,"; String[] split1 = s.split ...

  6. 通过Shell统计PV和UV

    PV.UV是网站分析中最基础.最常见的指标.PV即PageView,网站浏览量,指页面的浏览次数,用以衡量网站用户访问的网页数量.用户没打开一个页面便记录1次PV,多次打开同一页面则浏览量累计:UV即 ...

  7. Actuator监控器

    一.简介 Actuator(激励者;执行器)是Spring Boot提供的一个可挺拔模块,用于对工程进行监控.其通过不同的监控终端实现不同的监控功能.其功能与Dubbo的监控中心类似,不同的是,Dub ...

  8. List如何一边遍历,一边删除?

    1.新手常犯的错误 可能很多新手(包括当年的我,哈哈)第一时间想到的写法是下面这样的: public static void main(String[] args) { List<String& ...

  9. 1.Thmeleaf模板引擎

    1.Thmeleaf的基本语法 大部分的Thmeleaf表达式都直接被设置到HTML节点中,作为HTML节点的一个属性存在,这样同一个模板文件既可以使用浏览器直接打开,也可以放到服务器中用来显示数据, ...

  10. 数据恢复binlog2sql

    目录 一.原理及其使用 用途 闪回原理简析 binlog 有三种可选的格式: 来实例演习下来实例演习下 二.准备工作 一.原理及其使用 生产上误删数据.误改数据的现象也是时常发生的现象,作为运维这时候 ...