K - Painful Bases 状压dp
Painful Bases
这个题目一开始看,感觉有点像数位dp,但是因为是最多有16进制,因为限制了每一个数字都不同最多就有16个数。
所以可以用状压dp,看网上题解是
dp[s][r] 表示数字集合为s,对 k 取余为r的方案数。
这个题目首先把给你的字符转化成数字,然后枚举状态,枚举模数,枚举每一位的所有可能。
这个注意是写离散化的,不是直接暴力,虽然理论上都会超时,但是实际上离散化的没有超时。
这个题目我觉得还挺好的,以后可以在写写
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
ll dp[ << ][];
char s[];
int num[]; int main()
{
int t;
scanf("%d", &t);
for(int cas=;cas<=t;cas++)
{
int base, k;
scanf("%d%d", &base, &k);
scanf("%s", s + );
int len = strlen(s + );
for(int i=;i<=len;i++)
{
if (s[i] <= ''&&s[i] >= '') num[i] = s[i] - '';
else num[i] = + s[i] - 'A';
}
memset(dp, , sizeof(dp));
dp[][] = ;
for(int i=;i<(<<len);i++)
{
for(int mod=;mod<k;mod++)
{
if (dp[i][mod] == ) continue;
for(int id=;id<=len;id++)
{
int tmp = ( << (id-));
if ((tmp | i) == i) continue;
tmp |= i;
dp[tmp][(mod*base + num[id]) % k] += dp[i][mod];
}
}
}
printf("Case %d: %lld\n", cas,dp[( << len) - ][]);
}
return ;
}
状压dp
排列perm
这个题目和上面的差不多,有一个小地方注意一下,就是这个有相同的所以要除去。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#define sum 1<<10
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 4e5 + ;
ll dp[ << ][];
int a[maxn];
int vis[];
char s[maxn]; int main() {
int t;
scanf("%d", &t);
while (t--) {
int d;
scanf("%s%d", s + , &d);
memset(vis, , sizeof(vis));
int len = strlen(s + );
int tmp = ;
for (int i = ; i <= len; i++) {
a[i] = s[i] - '';
tmp |= ( << a[i]);
vis[a[i]]++;
}
ll ans = ;
for(int i=;i<=;i++)
{
if(vis[i])
{
for(int j=;j<=vis[i];j++)
{
ans *= j;
}
}
}
memset(dp, , sizeof(dp));
dp[][] = ;
for (int i = ; i < ( << len); i++) {
for (int mod = ; mod < d; mod++) {
if (dp[i][mod] == ) continue;
for (int j = ; j <= len; j++) {
int tmp1 = ( << (j - ));
if ((tmp1 | i) == i) continue;
tmp1 |= i;
dp[tmp1][(mod * + a[j]) % d] += dp[i][mod];
}
}
}
printf("%lld\n", dp[(<<len)-][]/ans);
}
return ;
}
状压
K - Painful Bases 状压dp的更多相关文章
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
- 【BZOJ1087】 [SCOI2005]互不侵犯King 状压DP
经典状压DP. f[i][j][k]=sum(f[i-1][j-cnt[k]][k]); cnt[i]放置情况为i时的国王数量 前I行放置情况为k时国王数量为J #include <iostre ...
随机推荐
- php.ini中文详解
[PHP] ;;;;;;;;;;;;;;;;;;;;;;; ; 关于 php.ini 配置文件 ; ;;;;;;;;;;;;;;;;;;;;;;; ; PHP 的初始化文件, 必须命名为 php.in ...
- redis的安装(ubuntu版本)
1.使用apt-get命令进行安装 安装gcc依赖 root@yatces-virtual-machine:~# apt-get update root@yatces-virtual-machine: ...
- lua 逻辑运算 and, or, not
这边并非说lua低级,为了方便区分才这么写的. 高级语言中的逻辑运算符是&&,||,! a&&b : 当a和b都为真, 结果返回为真,当a或者b有一个为假,结果返回为假 ...
- Python - 实现文件名自动更改,避免同名文件被覆盖的两个解决方法
[原创]转载请注明作者Johnthegreat和本文链接. 在一些不多的数据下载和生成的时候,我们倾向于直接保存为文件,当我们修改某些参数后再一次运行时,之前运行时生成的文件就被覆盖了.为了解决这个问 ...
- 2、flink入门程序Wordcount和sql实现
一.DataStream Wordcount 代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 基于scala实现 maven依赖如下: <depend ...
- TP5 JSON对象数组转换为普通数组
来源于:https://blog.csdn.net/lingchen__/article/details/67671047 使用TP5框架做项目时,对于数据的查询返回的都是对象,虽然也可以当做普通的数 ...
- php正则匹配到字符串里面的a标签
$cont = preg_replace('/<a href=\"(.*?)\".*?>(.*?)<\/a>/i','',$cont);
- Oracle 11g 精简客户端
通常开发人员会装上一个 oracle客户端,但一般不会在自己的机器上安装Oracle database Oracle 客户端安装体积很大,但是装上去了基本上就用2个功能:TNS配置服务名和sqlplu ...
- openssl 查看证书
查看证书 # 查看KEY信息 > openssl rsa -noout -text -in myserver.key # 查看CSR信息 > openssl req -noout -tex ...
- [Qt]执行cmd命令
要加 /c 参数 QProcess p; p.start("cmd", QStringList()<<"/c"<<"ping ...