Hdu-5787 K-wolf Number(数位DP)

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.

1LR1e18
2K5
 

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值