HDU 5787 K-wolf Number (数位DP)
K-wolf Number
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5787
Description
Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation of x is pairwised different.
Given (L,R,K), please count how many K-wolf numbers in range of [L,R].
Input
The input contains multiple test cases. There are about 10 test cases.
Each test case contains three integers L, R and K.
1≤L≤R≤1e18
2≤K≤5
Output
For each test case output a line contains an integer.
Sample Input
1 1 2
20 100 5
Sample Output
1
72
Source
2016 Multi-University Training Contest 5
##题意:
找出区间[L,R]中有多少个数满足任意相邻的K位均不不相同.
##题解:
数位DP:分别对l-1.r求出从0开始一共有多少个数满足条件.
dp[i][j]:处理到还剩下i个数时左边相邻k个数是j(j代表一串数)的情况种数.
用map, LL> dp[maxn]来表示dp数组,vector存储左边相邻的k个数.
依次枚举每一位可能放置的数字并进行递归处理.
1. 在递归时要标记一下之前放置的那些数能否保证小于上限,如果可以当前位可以放置0-9任意数.
2. 注意处理前导零和非前导零的情况:这里用-1代表前导零,如果枚举到当前位为0时,要先看上一位是否为-1,如果是-1则当前位要更新为-1(也是前导零).
3. 记忆化:用map-dp记录下当前的计算结果. 注意:仅当当前数能确定比上限小时才能记录dp值.
(反例:比如样例的20和100,先处理100得到dp[2][-1,-1,-1]=91, 若记录下当前dp,在处理19时,则会直接返回91.)
4. 之前一直TLE是因为每次处理数据时都把dp初始化了一遍,而实际上对于所有数据dp都可以共用,只需要初始化一次即可.
5. 看到一份用五维dp数组记录的代码仅用了300ms,而上述用map-vector的记录方式用了2000ms.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 55000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int k;
int num[20];
map<vector, LL> dp[20];
bool is_ok(const vector& cur) {
int state = 0;
for(int i=0; i<k; i++) {
if(cur[i] == -1) continue;
if(state & (1<<cur[i])) return false;
else state |= (1<<cur[i]);
}
return true;
}
LL dfs(int len, vector cur, bool is_small) {
if(len == 0) return 1LL;
if(is_small && dp[len].count(cur)) return dp[len][cur];
int limits = is_small? 9:num[len];
vector<int> next; next.clear();
int sz = cur.size();
for(int i=1; i<sz; i++) {
next.push_back(cur[i]);
}
LL ret = 0;
for(int i=0; i<=limits; i++) {
if(i) next.push_back(i);
else {
if(next[k-2] == -1) next.push_back(-1);
else next.push_back(0);
}
if(is_ok(next)) {
ret += dfs(len-1, next, !(!is_small&&i==limits));
}
next.pop_back();
}
if(is_small) dp[len][cur] = ret;
return ret;
}
LL solve(LL x) {
int cnt = 0;
vector cur; cur.clear();
while(x) {
num[++cnt] = x % 10;
x /= 10;
}
for(int i=0; i<k; i++)
cur.push_back(-1);
return dfs(cnt, cur, 0);
}
int main(int argc, char const *argv[])
{
//IN;
LL l,r;
for(int i=0; i<20; i++) dp[i].clear();
while(scanf("%I64d %I64d %d", &l,&r,&k) != EOF)
{
//for(int i=0; i<20; i++) dp[i].clear();
printf("%I64d\n", solve(r) - solve(l-1));
}
return 0;
}
HDU 5787 K-wolf Number (数位DP)的更多相关文章
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
- HDU 5787 K-wolf Number 数位DP
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
- HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛
题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- iOS学习笔记:frame,bound,center, anchorPoint
frame: View在它的Super View坐标系里的坐标 bound: 用来定义View自身坐标系和边界的Rect,Rect的原点表示View自身坐标系的原点坐标.举个例子: 一般情况下boun ...
- hdu 1243 反恐训练营(dp 最大公共子序列变形)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1243 d[i][j] 代表第i 个字符与第 j 个字符的最大的得分.,, 最大公共子序列变形 #inclu ...
- ASP.NET MVC 学习5、登陆页面改为SSO验证
单点登录(SSO,single sign-on)是一个会话或用户身份验证过程,用户只需要登录一次就可以访问所有相互信任的应用系统,二次登录时无需重新输入用户名和密码.简化账号登录过程并保护账号和密码安 ...
- HDU 1358 (所有前缀中的周期串) Period
题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出 ...
- IIS Server is too busy 解决方法(IIS6)
Server is too busy意思是服务器繁忙,资源不够用 为什么会出现这个问题呢? 因为服务器的配置不同,所能承受的压力不同. 而服务器默认对链接数,线程数等有设置,但这个设置太小,基本不够用 ...
- memcache的最佳实践方案。
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- erl0004 - ets 安全遍历
safe_fixtable(Tab, true|false) -> true Types: Tab = tid() | atom() 锁定set,bag和 ...
- 移动对meta的定义
以下是meta每个属性详解 尤其要注意的是content里多个属性的设置一定要用分号+空格来隔开,如果不规范将不会起作用. 一.<meta http-equiv="Content-Ty ...
- Mysql线程池优化笔记
Mysql线程池优化我是总结了一个站长的3篇文章了,这里我整理到一起来本文章就分为三个优化段了,下面一起来看看. Mysql线程池系列一(Thread pool FAQ) 首先介绍什么是mys ...
- 在stm32上移植wpa_supplicant(一)
wifi芯片为88w8686,已经写好了驱动,用的是SPI方式,接下来准备移植wpa_supplicant.参考的资料为一篇论文----<基于微控制器的WPA技术研究与应用>. wpa_s ...