HDU 5787 K-wolf Number 数位DP

K-wolf Number




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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5+5, M = 6e4+5, mod = 1e9+7, inf = 1e9+1000;
typedef long long ll;

ll L,R;
ll k,d[N],K;

ll dp[20][12][12][12][12];
bool vis[20][12][12][12][12];

ll dfs(int dep,int f,int now[],int bo) {
    int x = now[0], y = now[1], z = now[2], e = now[3];
    if(dep < 0) return 1;
    if(f&&vis[dep][x][y][z][e]) return dp[dep][x][y][z][e];
    if(f) {

        vis[dep][x][y][z][e] = true;
        ll& ret = dp[dep][x][y][z][e];

        for(int i = 0; i <= 9; ++i) {
            int OK = 1;
            for(int j = 0; j < k-1; ++j) if(now[j] == i) {OK = 0; break;}
            if(OK == 0) continue;
            if(!bo && !i)  ret += dfs(dep-1,f,now,bo);
            else {
                int tmpnow[6];
                for(int j = 0; j <= 4; ++j) tmpnow[j] = now[j+1];
                tmpnow[5] = 10;
                tmpnow[k-2] = i;
                ret += dfs(dep-1,f,tmpnow,bo||i);
            }
        }
        return ret;

    }else {
        ll ret = 0;
        for(int i = 0; i <= d[dep]; ++i) {

            int OK = 1;
            for(int j = 0; j < k-1; ++j) if(now[j] == i) {OK = 0; break;}
            if(OK == 0) continue;
            if(!bo && !i)  ret += dfs(dep-1,i<d[dep],now,bo);
            else {

                int tmpnow[6];
                for(int j = 0; j <= 4; ++j) tmpnow[j] = now[j+1];
                tmpnow[5] = 10;
                tmpnow[k-2] = i;
                ret += dfs(dep-1,i<d[dep],tmpnow,bo||i);
            }

        }
        return ret;
    }
}

ll solve(ll x) {
    //if(x < 0) return 0;
    memset(vis,false,sizeof(vis));
    memset(dp,0,sizeof(dp));
    int len = 0;
    while(x) {
        d[len++] = x % 10;
        x /= 10;
    }
    int now[6];
    for(int i = 0; i <= 5; ++i) now[i] = 10;
    return dfs(len-1,0,now,0);
}


int main()
{

    while(~scanf("%I64d%I64d%I64d",&L,&R,&k)) {
        //K = pre[k];
        printf("%I64d\n",solve(R) - solve(L-1));
    }

    /* ll x;
     while(~scanf("%I64d%d",&x,&k)) {
        K = pre[k];
        printf("%I64d\n",solve(x));
    }
    */

}

 

posted @ 2016-08-04 18:57  meekyan  阅读(244)  评论(0编辑  收藏  举报