LightOJ 1021 Painful Bases
题目:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1021
题意:给你一个每位都不相同的base进制数,求对它排列组合后%k等于0的种数
(xy)base % k = ( x*base+y ) % k = (( x%k ) * base + y) % k
利用这个公式,从最高位开始枚举状态,另一维枚举余数就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<map> #include<set> using namespace std; char s[20]; int a[20]; long long dp[1<<16][20]; int main() { int T; scanf ( "%d" ,&T); for ( int ca=1;ca<=T;ca++) { int base,k; scanf ( "%d%d" ,&base,&k); scanf ( "%s" ,s); int len= strlen (s); for ( int i=0;i<len;i++) a[i]=s[i]>= 'A' ?s[i]- 'A' +10:s[i]- '0' ; memset (dp,0, sizeof (dp)); dp[0][0]=1; int tot=1<<len; for ( int i=0;i<tot;i++) { for ( int j=0;j<k;j++) { if (!dp[i][j]) continue ; for ( int l=0;l<len;l++) { if (i>>l&1) continue ; dp[i|1<<l][(j*base+a[l])%k]+=dp[i][j]; } } } printf ( "Case %d: %lld\n" ,ca,dp[tot-1][0]); } return 0; } |
分类:
状压dp
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!