1021 - Painful Bases
2016-06-20 19:20
549 查看
1021 - Painful Bases
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.
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.
题意:给你的n为进制,后面的m为模数,然后给你一串数字为当前进制下的数,问你拆分这个数,然后再全排列组成新的数,问这些数中有多少是m的倍数;
思路:状态压缩dp;
dp[i][j]表示在i状态下对m取模为j的种数;我们可以将这些数组合,那么种数就是2n,然后每一种组合就是一种状态,那么每种状态下可能的模数有m-1种,那么咋实现全排列,
全排列就是组合数承n!;那么每种状态可以由前面的状态推来那么这就是全排列的过程,只不过将相同的合并。
状态转移方程看下面代码:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
思路:状态压缩dp;
dp[i][j]表示在i状态下对m取模为j的种数;我们可以将这些数组合,那么种数就是2n,然后每一种组合就是一种状态,那么每种状态下可能的模数有m-1种,那么咋实现全排列,
全排列就是组合数承n!;那么每种状态可以由前面的状态推来那么这就是全排列的过程,只不过将相同的合并。
状态转移方程看下面代码:
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<stdlib.h> #include<math.h> #include<queue> #include<stack> #include<vector> using namespace std; typedef long long LL; char ans[100]; int shu[100]; LL dp[1<<16+1][22]; int main(void) { int i,j,k; scanf("%d",&k); int s; for(s=1; s<=k; s++) { int n,m; scanf("%d %d",&n,&m); scanf("%s",ans); int l=strlen(ans); for(i=0; i<l; i++) { if(ans[i]>='0'&&ans[i]<='9') { shu[i]=ans[i]-'0'; } else { shu[i]=ans[i]-'A'+10; } } memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=0; i<(1<<l); i++) { for(j=0; j<=m-1; j++) { for(int s=0; s<l; s++) { int ak=j; if(!(i&(1<<s))) { dp[i|(1<<s)][(ak*n+shu[s])%m]+=dp[i][j]; } } } } printf("Case %d: %lld\n",s,dp[(1<<l)-1][0]); } return 0; }
相关文章推荐
- 马斯克的AI野心——OpenAI Gym系统深度解析
- [Leetcode]Nim Game
- Block Chain 区块链
- 谷歌约束控件(ConstraintLayout)扁平化布局入门
- 了解使用Android ConstraintLayout (Google I/O 2016)
- 人工智能革命:人类将永生或者灭绝
- 性能瞬间飙升!教你如何组RAID0磁盘阵列
- 性能瞬间飙升!教你如何组RAID0磁盘阵列
- extmail日常维护
- hdu 1021 Fibonacci Again
- 写email那些事
- 【leetcode】217. Contains Duplicate
- raid0+磁盘加密
- DevExpress Grid中实现点击Detail获得Master rowHandle的方法
- Rails 5 有什么新特性?
- software RAID0+cryptsetup磁盘加密
- Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别
- 解决maillog文件删除后,空间不释放
- 机器学习之(二)OpenAI 生成模型聚焦无监督学习
- 文本查看命令 - head和tail