数位DP -启示录

http://poj.org/problem?id=3208

一个魔鬼数为包含连续三个666的的数字,给个n(n<5e7)求第n个魔鬼数。

预处理f[i][j],f[i][3]表示由前i位数字构成所有可能的魔鬼数,需要注意这里允许前导0存在。

f[i][2]表示由i位数字构成的开头为2个6的非魔鬼数个数,

f[i][1]表示由i位数字构成的开头为1个6的非魔鬼数个数,

f[i][1]表示由i位数字构成的开头为0个6的非魔鬼数个数,

之后从高位到低位填每个数,每个数从小到大枚举。

#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
LL f[18][5];
void init()
{
//    f[1][1]=1;
//    f[1][0]=9;
    f[0][0]=1;
    for(int i=1; i<=15; i++)
    {
        f[i][0]=(f[i-1][2]+f[i-1][1]+f[i-1][0])*9;
        f[i][1]=f[i-1][0];
        f[i][2]=f[i-1][1];
        f[i][3]=f[i-1][2]+f[i-1][3]*10;
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    init();
//    for(int i=1; i<=15; i++)
//        printf("%d ",f[i][3]);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int p;
        for(p=1; f[p][3]<n; p++);
        int cnk=0;
        for(; p>=1; p--)
        {
            for(int j=0; j<=9; j++)
            {
                int sum=0;
                if(j==6||cnk>=3)
                {
                    int t=cnk+1;
                    if(t>=3) sum+=f[p-1][0]+f[p-1][1]+f[p-1][2];
                    else if(t==2) sum+=f[p-1][1]+f[p-1][2];
                    else sum+=f[p-1][2];
                    sum+=f[p-1][3];
                }
                else
                    sum=f[p-1][3];
                if(sum<n)
                    n-=sum;
                else
                {
                    if(j==6)
                        cnk++;
                    else if(cnk<3)
                        cnk=0;
                    printf("%d",j);
                    break;
                }
            }
        }
        printf("\n");
    }
}

猜你喜欢

转载自www.cnblogs.com/dongdong25800/p/10778150.html