您的位置:首页 > 其它

HDU 5787 K-wolf Number 数位DP

2016-08-04 18:57 337 查看

K-wolf Number

[align=left][b]Problem Description[/b][/align]
[align=left] [/align]
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].

[align=left]Input[/align]
[align=left] [/align]
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

[align=left][b]Output[/b][/align]
[align=left] [/align]
For each test case output a line contains an integer.

[align=left][b]Sample Input[/b][/align]
[align=left] [/align]

1 1 2
20 100 5

[align=left][b]Sample Output[/b][/align]
[align=left] [/align]

1
72

[align=left][b]题意[/b]:[/align]
[align=left]  询问 [L,R] 间有多少个数 满足 连续k位数都不相同[/align]
[align=left][b]题解[/b]:[/align]
[align=left]  数位DP[/align]
[align=left]  传递 前 k-1 分别是什么[/align]
[align=left] [/align]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5+5, M = 6e4+5, mod = 1e9+7, inf = 1e9+1000;
typedef long long ll;

ll L,R;
ll k,d
,K;

ll dp[20][12][12][12][12];
bool vis[20][12][12][12][12];

ll dfs(int dep,int f,int now[],int bo) {
int x = now[0], y = now[1], z = now[2], e = now[3];
if(dep < 0) return 1;
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 = 0; i <= 9; ++i) {
int OK = 1;
for(int j = 0; j < k-1; ++j) if(now[j] == i) {OK = 0; break;}
if(OK == 0) continue;
if(!bo && !i)  ret += dfs(dep-1,f,now,bo);
else {
int tmpnow[6];
for(int j = 0; j <= 4; ++j) tmpnow[j] = now[j+1];
tmpnow[5] = 10;
tmpnow[k-2] = i;
ret += dfs(dep-1,f,tmpnow,bo||i);
}
}
return ret;

}else {
ll ret = 0;
for(int i = 0; i <= d[dep]; ++i) {

int OK = 1;
for(int j = 0; j < k-1; ++j) if(now[j] == i) {OK = 0; break;}
if(OK == 0) continue;
if(!bo && !i)  ret += dfs(dep-1,i<d[dep],now,bo);
else {

int tmpnow[6];
for(int j = 0; j <= 4; ++j) tmpnow[j] = now[j+1];
tmpnow[5] = 10;
tmpnow[k-2] = i;
ret += dfs(dep-1,i<d[dep],tmpnow,bo||i);
}

}
return ret;
}
}

ll solve(ll x) {
//if(x < 0) return 0;
memset(vis,false,sizeof(vis));
memset(dp,0,sizeof(dp));
int len = 0;
while(x) {
d[len++] = x % 10;
x /= 10;
}
int now[6];
for(int i = 0; i <= 5; ++i) now[i] = 10;
return dfs(len-1,0,now,0);
}

int main()
{

while(~scanf("%I64d%I64d%I64d",&L,&R,&k)) {
//K = pre[k];
printf("%I64d\n",solve(R) - solve(L-1));
}

/* ll x;
while(~scanf("%I64d%d",&x,&k)) {
K = pre[k];
printf("%I64d\n",solve(x));
}
*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: