K-wolf Number
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1026 Accepted Submission(s): 388
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].
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
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
Author
ZSTU
Source
Recommend
wange2014
思路:很明显示数位dp,dp思路是开到5维,第一维表示位数,后面四维分别表示前4个数,注意一个问题就是前导0,这里前导0用10表示,非前导0的0就用0表示,然后化简成一般的数位dp问题了,下面给代码。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL dp[20][11][11][11][11];
int num[20], k;
bool check(int x,int p1,int p2,int p3,int p4){
if (k == 2)
return x != p4;
if (k == 3)
return x != p4&&x != p3;
if (k == 4)
return x != p4&&x != p3&&x != p2;
if (k == 5)
return x != p4&&x != p3&&x != p2&&x != p1;
}
LL dfs(int pos, int p1, int p2, int p3, int p4, int limit){
if (pos < 1)
return 1;//因为l和r的范围是大于0的所以不需要考虑特别情况了,直接返回1就好
if (!limit&&~dp[pos][p1][p2][p3][p4])
return dp[pos][p1][p2][p3][p4];
int end = limit ? num[pos] : 9;
LL ans = 0;
for (int i = 0; i <= end; i++){
if (!i&&p4 == 10){//前一位是前导0那么前面的肯定都是前导0
ans += dfs(pos - 1, 10, 10, 10, 10, limit&&i == end);
}
else if (check(i, p1, p2, p3, p4)){
ans += dfs(pos - 1, p2, p3, p4, i, limit&&i == end);
}
}
dp[pos][p1][p2][p3][p4] = ans;
return ans;
}
LL solve(LL x){
int len = 0;
while (x){
num[++len] = x % 10;
x /= 10;
}
return dfs(len, 10, 10, 10, 10, 1);
}
int main(){
LL l, r;
while (~scanf("%lld%lld%d", &l, &r, &k)){
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(r) - solve(l - 1));
}
}