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 ...
随机推荐
- loadrunner post请求
注意:loadrunner参数中的引号,需要自己加"\" post 请求,分为header 和body两个部分处理 header部分比较容易处理,使用函数实现,如web_add_h ...
- SpringCloud-服务注册中心「Eureka」的介绍与使用
Eureka 两大组件
- Daily Scrum 1/4/2015
Process: After New year's Day, we start our project in plan. Zhanyang: Add some useful UI in the IOS ...
- Daily Scrum 12/23/2015
Process: Zhaoyang: Compile the Caffe IOS version and make it run in the IOS9. Yandong: Finish the Az ...
- 编写高质量Python程序(四)库
本系列文章为<编写高质量代码--改善Python程序的91个建议>的精华汇总. 按需选择 sort() 或者 sorted() Python 中常用的排序函数有 sort() 和 sort ...
- 胜利大逃亡 BFS
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0 ...
- 百度智能云虚拟主机 Typecho 分类功能失效 | 开启伪静态地址
出现的问题 $this->is() 方法失效,无法正确判断 archive.category.tags 页面类型. 点击分类页面.归档页面时,虽然 URL 是正确的,但网页内容却是 index. ...
- 关于MIME类型问题,浏览器请求到的资源是乱码
简介 我想很多同学都可能会遇到这样的问题,调用后台提共的静态资源服务api时,用浏览器打开发现却是一堆乱码.需要的是 JSON, 拿到的却是 xml,访问一个mp4的文件,浏览器直接下载.这一切的来源 ...
- 2020年必须掌握的硬核技能k8s
Kubernetes 是一个软件系统,使你在数以万计的电脑节点上运行软件时就像 所有节点是以单个大节点一样, 它将底层基础设施抽象,这样做同时简化了应用开发.部署,以及对开发和运维团队的管理. Kub ...
- Redis来限制用户 ------------IP某个时间段内访问的次数
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); //获取客户端真实ip地址 function get_real_ip(){ s ...