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)的更多相关文章

  1. 多校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 用作标记,当现在枚举的数小 ...

  2. HDU.4352.XHXJ's LIS(数位DP 状压 LIS)

    题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...

  3. 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 ...

  4. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  5. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  7. 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 ...

  8. HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...

  9. 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】

    Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  10. 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 ...

随机推荐

  1. BZOJ 1004 Cards(Burnside引理+DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1004 题意:三种颜色的扑克牌各有Sr,Sb,Sg张.给出m种置换.两种染色方案在某种置换 ...

  2. 面试题_66_to_75_Java IO 和 NIO 的面试题

    IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...

  3. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  4. Liunx常用的特殊环境变量

    [weiqiang.liu@l~]$ sh variable xiaoqiang xiaoxuenumber:2scname:variablefirst:xiaoqiangsecond:xiaoxue ...

  5. codeforces 333B - Chips

    注意:横向纵向交叉时,只要两条边不是正中的边(当n&1!=1),就可以余下两个chip. 代码里数组a[][]第二维下标 0表示横向边,1表示纵向边. #include<stdio.h& ...

  6. highcharts 柱状图动态设置数据应用实例

    <div id="container" style="min-width:700px;height:400px"></div> #jav ...

  7. Java [Leetcode 112]Path Sum

    题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  8. 02day2

    油滴扩展 [问题描述] 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能 ...

  9. 《C++ Primer 4th》读书笔记 第4章-数组和指针

    原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3911573.html

  10. OSGI框架学习

    OSGI框架三个重要概念 OSGi框架是根据OSGi规范中定义的三个概念层设计的:模块.模块生命周期.服务. 模块层定义了OSGi模块的概念(bundle,即包含一个元数据MANIFEST.MF的JA ...