多校5 HDU5787 K-wolf Number 数位DP

时间:2024-01-10 13:13:26
 // 多校5 HDU5787 K-wolf Number 数位DP
// dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d
// f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小
// 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位
// http://blog.csdn.net/weizhuwyzc000/article/details/52097690 // #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
const double inf = 0x3f3f3f3f;
#define lson l,mid,rt<<1
// #define rson mid+1,r,rt<<1|1
const int N = ;
const int M = 1e6+;
const int MOD = 1e9+;
#define LL long long
#define LB long double
// #define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre(){freopen("in.txt","r",stdin);}
void freout(){freopen("out.txt","w",stdout);}
inline int read(){int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}while(ch>=''&&ch<='') { x=x*+ch-'';ch=getchar();}return x*f;} LL l,r,cnt;
int k;
int dig[];
LL dp[][][][][][];
int vis[][][][][][];
int cas;
LL dfs(LL pos,int a,int b,int c,int d,int f){
if(pos==cnt) return 1LL;
if(vis[pos][a][b][c][d][f]==cas) return dp[pos][a][b][c][d][f];
vis[pos][a][b][c][d][f]=cas;
LL ans=;
if(f){
for(int i=;i<;i++){
if(k==) {if(d==i) continue;}
else if(k==) {if(d==i||c==i) continue;}
else if(k==) {if(d==i||c==i||b==i) continue;}
else {if(d==i||c==i||b==i||a==i) continue;}
if(i==){
if(d==) ans+=dfs(pos+,a,b,c,d,f);
else ans+=dfs(pos+,b,c,d,i,f);
}
else ans+=dfs(pos+,b,c,d,i,f);
}
}
else{
for(int i=;i<;i++){
if(k==) {if(d==i) continue;}
else if(k==) {if(d==i||c==i) continue;}
else if(k==) {if(d==i||c==i||b==i) continue;}
else {if(d==i||c==i||b==i||a==i) continue;}
if(i<dig[pos]){
if(i==){
if(d==) ans+=dfs(pos+,a,b,c,d,);
else ans+=dfs(pos+,b,c,d,i,);
}
else ans+=dfs(pos+,b,c,d,i,);
}
else if(i==dig[pos]){
if(i==){
if(d==) ans+=dfs(pos+,a,b,c,d,f);
else ans+=dfs(pos+,b,c,d,i,f);
}
else
ans+=dfs(pos+,b,c,d,i,f);
}
}
}
return dp[pos][a][b][c][d][f]=ans;
} bool ck(){
for(int i=;i<cnt;i++){
for(int j=i-;j>=max(,i-k+);j--){
if(dig[i]==dig[j]) return false;
}
}
return true;
}
int tem[];
int main(){
while(~scanf("%I64d%I64d%d",&l,&r,&k)){
cnt=;
while(r) tem[cnt++]=r%,r=r/;
int k=;
for(int i=cnt-;i>=;i--) dig[k++]=tem[i];
cas++;
LL ans1=dfs(,,,,,);
cnt=,k=;
while(l) tem[cnt++]=l%,l=l/; for(int i=cnt-;i>=;i--) dig[k++]=tem[i];
cas++;
LL ans2=dfs(,,,,,);
if(ck()) ans2--;
printf("%I64d\n",ans1-ans2);
}
return ;
}