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

 

 

转载于:https://www.cnblogs.com/jhz033/p/6596398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值