K-wolf Number


Problem 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
 
题意
  询问 [L,R] 间有多少个数 满足 连续k位数都不相同
题解
  数位DP
  传递 前 k-1 分别是什么
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. #pragma comment(linker, "/STACK:102400000,102400000")
  8. const int N = 1e5+, M = 6e4+, mod = 1e9+, inf = 1e9+;
  9. typedef long long ll;
  10.  
  11. ll L,R;
  12. ll k,d[N],K;
  13.  
  14. ll dp[][][][][];
  15. bool vis[][][][][];
  16.  
  17. ll dfs(int dep,int f,int now[],int bo) {
  18. int x = now[], y = now[], z = now[], e = now[];
  19. if(dep < ) return ;
  20. if(f&&vis[dep][x][y][z][e]) return dp[dep][x][y][z][e];
  21. if(f) {
  22.  
  23. vis[dep][x][y][z][e] = true;
  24. ll& ret = dp[dep][x][y][z][e];
  25.  
  26. for(int i = ; i <= ; ++i) {
  27. int OK = ;
  28. for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
  29. if(OK == ) continue;
  30. if(!bo && !i) ret += dfs(dep-,f,now,bo);
  31. else {
  32. int tmpnow[];
  33. for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
  34. tmpnow[] = ;
  35. tmpnow[k-] = i;
  36. ret += dfs(dep-,f,tmpnow,bo||i);
  37. }
  38. }
  39. return ret;
  40.  
  41. }else {
  42. ll ret = ;
  43. for(int i = ; i <= d[dep]; ++i) {
  44.  
  45. int OK = ;
  46. for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
  47. if(OK == ) continue;
  48. if(!bo && !i) ret += dfs(dep-,i<d[dep],now,bo);
  49. else {
  50.  
  51. int tmpnow[];
  52. for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
  53. tmpnow[] = ;
  54. tmpnow[k-] = i;
  55. ret += dfs(dep-,i<d[dep],tmpnow,bo||i);
  56. }
  57.  
  58. }
  59. return ret;
  60. }
  61. }
  62.  
  63. ll solve(ll x) {
  64. //if(x < 0) return 0;
  65. memset(vis,false,sizeof(vis));
  66. memset(dp,,sizeof(dp));
  67. int len = ;
  68. while(x) {
  69. d[len++] = x % ;
  70. x /= ;
  71. }
  72. int now[];
  73. for(int i = ; i <= ; ++i) now[i] = ;
  74. return dfs(len-,,now,);
  75. }
  76.  
  77. int main()
  78. {
  79.  
  80. while(~scanf("%I64d%I64d%I64d",&L,&R,&k)) {
  81. //K = pre[k];
  82. printf("%I64d\n",solve(R) - solve(L-));
  83. }
  84.  
  85. /* ll x;
  86. while(~scanf("%I64d%d",&x,&k)) {
  87. K = pre[k];
  88. printf("%I64d\n",solve(x));
  89. }
  90. */
  91.  
  92. }

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 5898 odd-even number 数位DP

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

  4. HDU 3709 Balanced Number (数位DP)

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

  5. HDU 5179 beautiful number 数位dp

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

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

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

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

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

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

  9. 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. liunux mysql MySQL表名不区分大小写的设置方法

    原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:1.用root登录,修改 /etc/my.cnf:2.在[mysqld]节点下,加入一行: lowe ...

  2. python模块名和文件名冲突解决

    对于python初学者,很容易练习到一个随机数生成的程序,代码如下: #!/usr/bin/python import random print(random.randint(12,20)) 这个小程 ...

  3. 设定报表变量的CharSpacing

    设定报表变量的CharSpacing字符间距,预览时都没问题, 间距大的字与字之间拉得比较大,但在大多数电脑打印时和预览的结果一样,但有些电脑打印出来却跟没有设间距一样?

  4. Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree

    解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...

  5. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  6. C#导出Excel动态列

    一.用StreamWrite流对象,导出Excel 1. string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagn ...

  7. [页面滚动到底部]jquery $(window).height()取值等于$(document).height()的问题

    问题现象:JSP中头部引用了某个head.jsp,在videoList.jsp中生成片段时如下 实际最终生成的HTML如下: <!DOCTYPE html>没有解析到,原因找到了,先想办法 ...

  8. [Android Pro] DES加密 version1

    reference to : http://blog.csdn.net/wfung_kwok/article/details/7766029 加密和解密要用同一個key AES: import jav ...

  9. MVC4 @RenderBody、@RenderSection、@RenderPage、Html.RenderPartial、Html.RenderAction的作用和区别

    1. RenderBody在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句:@Rend ...

  10. JUnit笔记