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

来源:互联网 发布:苹果cms 一键采集插件 编辑:程序博客网 时间:2024/05/17 01:29
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 220 100 5
 

Sample Output
172
 

题意:问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 100000using 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 0
原创粉丝点击