hdu 5787 K-wolf Number 数位dp

K-wolf Number

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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
 

 

Author
ZSTU
 

 

Source
题意链接:http://acm.hdu.edu.cn/showproblem.php?pid=5787
题意:找出区间与前面K个数不同的数的个数;
思路:数位dp,多开几维记录就是,trick:需要去除前导0的影响;
   本来想把那个开个11进制状态压缩一下,发现难写,可能是我代码能力差吧,还不如直接XJB写,更容易看;
   搓代码;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=3e4+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=2147493647;
ll f[30][12][12][12][12][2],bit[60];
int k;
ll dp(int pos,int a,int b,int c,int d,int p,int flag,int q)
{
    if(pos==0)return (p==0);
    if(flag&&f[pos][a][b][c][d][p]!=-1)return f[pos][a][b][c][d][p];
    int x=flag?9:bit[pos];
    ll ans=0;
    for(int i=0;i<=x;i++)
    {
        if(!q&&!i)
            ans+=dp(pos-1,10,10,10,10,p,flag||i<x,q);
        else
        {
            int mark=0;
            if(i==a||i==b||i==c||i==d)
                mark=1;
            if(k==2)
                ans+=dp(pos-1,i,10,10,10,p||mark,flag||i<x,q||i!=0);
            else if(k==3)
                ans+=dp(pos-1,i,a,10,10,p||mark,flag||i<x,q||i!=0);
            else if(k==4)
                ans+=dp(pos-1,i,a,b,10,p||mark,flag||i<x,q||i!=0);
            else
                ans+=dp(pos-1,i,a,b,c,p||mark,flag||i<x,q||i!=0);
        }
    }
    if(flag)f[pos][a][b][c][d][p]=ans;
    return ans;
}
ll getans(ll x)
{
    int len=0;
    while(x)
    {
        bit[++len]=x%10;
        x/=10;
    }
    return dp(len,10,10,10,10,0,0,0);
}
int main()
{
    ll l,r;
    while(~scanf("%lld%lld%d",&l,&r,&k))
    {
        memset(f,-1,sizeof(f));
        printf("%lld\n",getans(r)-getans(l-1));
    }
    return 0;
}

 

 
posted @ 2017-03-21 20:07  jhz033  阅读(274)  评论(0编辑  收藏  举报