HDU 5787 K-wolf Number 数位DP

时间:2024-01-10 12:58:44

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));
}
*/ }