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 分别是什么
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5+, M = 6e4+, mod = 1e9+, inf = 1e9+;
typedef long long ll; ll L,R;
ll k,d[N],K; ll dp[][][][][];
bool vis[][][][][]; ll dfs(int dep,int f,int now[],int bo) {
int x = now[], y = now[], z = now[], e = now[];
if(dep < ) return ;
if(f&&vis[dep][x][y][z][e]) return dp[dep][x][y][z][e];
if(f) { vis[dep][x][y][z][e] = true;
ll& ret = dp[dep][x][y][z][e]; for(int i = ; i <= ; ++i) {
int OK = ;
for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
if(OK == ) continue;
if(!bo && !i) ret += dfs(dep-,f,now,bo);
else {
int tmpnow[];
for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
tmpnow[] = ;
tmpnow[k-] = i;
ret += dfs(dep-,f,tmpnow,bo||i);
}
}
return ret; }else {
ll ret = ;
for(int i = ; i <= d[dep]; ++i) { int OK = ;
for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
if(OK == ) continue;
if(!bo && !i) ret += dfs(dep-,i<d[dep],now,bo);
else { int tmpnow[];
for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
tmpnow[] = ;
tmpnow[k-] = i;
ret += dfs(dep-,i<d[dep],tmpnow,bo||i);
} }
return ret;
}
} ll solve(ll x) {
//if(x < 0) return 0;
memset(vis,false,sizeof(vis));
memset(dp,,sizeof(dp));
int len = ;
while(x) {
d[len++] = x % ;
x /= ;
}
int now[];
for(int i = ; i <= ; ++i) now[i] = ;
return dfs(len-,,now,);
} int main()
{ while(~scanf("%I64d%I64d%I64d",&L,&R,&k)) {
//K = pre[k];
printf("%I64d\n",solve(R) - solve(L-));
} /* ll x;
while(~scanf("%I64d%d",&x,&k)) {
K = pre[k];
printf("%I64d\n",solve(x));
}
*/ }

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. shell变量判空几种方法

    强烈声明:关于对数字的比较以及判断是否为空 最好在外层添加""引起来,这样可以避免空与其他字符比较时报错的问题. 1. 变量通过" "引号引起来 #!/bin/ ...

  2. matrix_超时

    问题 H: matrix 时间限制: 1 Sec  内存限制: 256 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 给定两个长度为n的整数序列l和t,分别作为n×n矩阵F的第 ...

  3. 【leetcode】 Search a 2D Matrix (easy)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. Qt 获取cmd运行结果

    http://www.cnblogs.com/gisbeginner/archive/2012/12/08/2809063.html BOOL ExecDosCmd(){ #define EXECDO ...

  5. Java反射实战

    一.背景 最近的项目中需要使用到Java 反射的知识,以前不怎么了解,也基本没怎么用过,抽出一片时间,来具体学习和实战下Java的反射!拿来和大家分享以及记录方便以后学习! 二.反射相关概念解析 1. ...

  6. ssh-keygen详解

    先来一段google wiki关于ssh key的解释,对应的连接为:https://wiki.archlinux.org/index.php/SSH_keys_(%E7%AE%80%E4%BD%93 ...

  7. jquery获得select option的值 和对select option的操作

    jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...});   //为Se ...

  8. (1)第一个ASP.NET Web API

      Install-Package Microsoft.AspNet.WebApi . Global.asax protected void Application_Start() { AreaReg ...

  9. Python 小游戏 Bunny

    最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...

  10. jQuery ajax同步的替换方法,使用 $.Deferred()对象

    function aa() { var defer = $.Deferred(); $.ajax({ url: "/Handler1.ashx", type: "post ...