lightoj 1021 - Painful Bases 状压DP
2016-03-14 16:28
471 查看
给定一个base进制和一个模数k,然后给个base进制下的数字,问这些数字全排列中(在base进制下)是k的倍数有几个。
范围又是16,又是状压DP。
对于一个状态i来言,如果i&(1<<j)不为0的话,第j数字就有可能是最低位,那么我们只需要考虑之前的余数再加上第j个数字来判断当前余数,k只有20,那么枚举k的状态是可行的。
范围又是16,又是状压DP。
对于一个状态i来言,如果i&(1<<j)不为0的话,第j数字就有可能是最低位,那么我们只需要考虑之前的余数再加上第j个数字来判断当前余数,k只有20,那么枚举k的状态是可行的。
#include<bits/stdc++.h> using namespace std; #define ll long long ll dp[(1<<16)+3][22]; int num[22]; char s[22]; int main() { int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { int b,k; scanf("%d %d",&b,&k); scanf("%s",s); int len=strlen(s); for(int i=0;i<len;i++) num[i]=s[i]>='A'?(10+s[i]-'A'):s[i]-'0'; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=0;i<(1<<len)-1;i++) { for(int j=0;j<k;j++) { if(!dp[i][j]) continue; for(int x=0;x<len;x++) { if((i&(1<<x))==0) { dp[i|(1<<x)][(j*b+num[x])%k]+=dp[i][j]; } } } } printf("Case %d: %lld\n",cas,dp[(1<<len)-1][0]); } return 0; }
相关文章推荐
- System and device programming——R&W with semaphore
- An error was encountered while running(Domain=LaunchSerivcesError, Code=0)
- 发现大量的TIME_WAIT解决办法
- la 4394 string painter 区间dp
- 查看AIX文件系统分布所在的物理磁盘
- Flask-mail 邮件带中文名称的附件
- [AIR] Screen 的应用
- 简述raid0、raid1、raid5的区别
- IMA: maintain i_readcount in the VFS layer
- MRC时代@property关键字retain的作用
- need improve 219. Contains Duplicate II
- 217. Contains Duplicate
- 172. Factorial Trailing Zeroes
- 70. Climbing Stairs
- Leetcode 11. Container With Most Water
- Kafka High Availability
- 苹果Macbook Air怎么安装Win7系统图解教程(图)
- 我的思考,我的科技漫谈——人工智能
- 11 Container With Most Water
- Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction