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
题意:问l到r间存在多少相邻的k位都不想等的数。
分析:数位DP,保留前k-1位的状态,注意处理前导零。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 100000
using namespace std;
typedef long long ll;
int T,n,m,k,MOD,a[21];
ll l,r,dp[21][100005];
bool got(int x,int y,int lead)
{
int pos = 0;
while(x)
{
pos++;
if(x % 10 == y) return false;
x/= 10;
}
if(lead) return true;
if(pos != k-1 && !y) return false;
return true;
}
ll dfs(int pos,int sta,int lead,int limit)
{
if(pos == -1) return 1ll;
if(!limit && !lead && dp[pos][sta] >= 0) return dp[pos][sta];
int up = limit ? a[pos] : 9;
ll ans = 0;
for(int i = 0;i <= up;i++)
{
if(!got(sta,i,lead)) continue;
ans += dfs(pos-1,(sta*10+i)%MOD,lead && (sta*10 + i < (MOD/10)),limit && i == up);
}
if(!limit && !lead) dp[pos][sta] = ans;
return ans;
}
ll solve(ll x)
{
int pos = 0;
while(x)
{
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos-1,0,1,1);
}
int main()
{
while(~scanf("%I64d%I64d%d",&l,&r,&k))
{
memset(dp,-1,sizeof(dp));
MOD = 1;
for(int i = 1;i < k;i++) MOD *= 10;
printf("%I64d\n",solve(r)-solve(l-1));
}
}