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. Leetcode中的SQL题目练习(一)

    595. Big Countries https://leetcode.com/problems/big-countries/description/ Description name contine ...

  2. 单体内置对象 Global 和 Math

    单体内置对象 Global 和 Math 在所有代码执行前,作用域中就已经存在两个内置对象:Global(全局)和Math.在大多数ES实现中都不能直接访问Global对象.不过,WEB浏览器实现了承 ...

  3. Vue相关,vue父子组件生命周期执行顺序。

    一.实例代码 父组件: <template> <div id="parent"> <child></child> </div& ...

  4. C++构造函数和析构函数初步认识

    构造函数 1.构造函数与类名相同,是特殊的公有成员函数.2.构造函数无函数返回类型说明,实际上构造函数是有返回值的,其返回值类型即为构造函数所构建到的对象.3.当新对象被建立时,构造函数便被自动调用, ...

  5. 【STM32】WS2812介绍、使用SPI+DMA发送数据

    这篇要使用到SPI+DMA,需要了解的话,可以参考我另两篇博客 时钟:https://www.cnblogs.com/PureHeart/p/11330967.html SPI+DMA通信:https ...

  6. 4.2 rust 命令行参数

     从命令行读取参数 use std::env; fn main() { let args: Vec<String> = env::args().collect(); println!(&q ...

  7. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  8. spring boot 启动卡半天

    测试服务器到期,把环境切了,早上过来 ios 和 安卓 都说 测试环境连不上,ps -ef | grep app.jar 查看了一下进程,发现没有启动,于是 重新打包.部署,一顿骚操作后,监控启动日志 ...

  9. 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...

  10. 编译安装haproxy2.0

    先解决lua环境,(因为centos自带的版本不符合haproxy要求的最低版本(5.3)先安装Lua依赖的包 [root@slave-master lua-5.3.5]# yum install  ...