zoukankan      html  css  js  c++  java
  • 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
    ",getans(r)-getans(l-1));
        }
        return 0;
    }
     
  • 相关阅读:
    EF关联
    nopcommerce v3.9中文包
    Android Activity切换与Activity间数据交互
    C#多线程的用法9-Semaphore
    C#多线程的用法8-线程间的协作AutoResetEvent
    C#多线程的用法7-线程间的协作ManualResetEvent
    C#多线程的用法6-线程间的协作Mutex
    C#多线程的用法5-线程间的协作Monitor
    C#多线程的用法4-线程间的协作lock快捷方式
    C#多线程的用法3-线程间的协作Join
  • 原文地址:https://www.cnblogs.com/jhz033/p/6596398.html
Copyright © 2011-2022 走看看