【数位DP】【hdu 3943】K-th Nya Number

本文深入探讨了递归和动态规划算法的核心概念、应用实例及优化技巧,旨在帮助读者掌握解决复杂问题的高效方法。

http://acm.hdu.edu.cn/showproblem.php?pid=3943


被虐哈哈哈

该练递推了


//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const LL read()
{LL r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
LL x,y,l,r;
LL dp[25][25][25];
int bit[25];
/////////////////////////////////////////////////
void predoing()
{
	MS(dp,0);
	dp[0][0][0]=1;
	rep(i,0,19)
	{
		rep(j,0,i) rep(k,0,i-j)
		{
			//枚举第i+1位有十种情况 
			dp[i+1][j][k]+=dp[i][j][k]*8;
			dp[i+1][j+1][k]+=dp[i][j][k];
			dp[i+1][j][k+1]+=dp[i][j][k];
	    }
	}
}
LL get_count(LL n)
{
	n++;
	for(*bit=0;n;n/=10) bit[++*bit]=n%10;
	LL ans=0;
	int cx=x,cy=y;
	per(i,*bit,1)
	{
		rep(j,0,bit[i]-1)
		{
			if(j==4)
			{
				if(cx) ans+=dp[i-1][cx-1][cy];
			}
			else if(j==7)
			{
				if(cy) ans+=dp[i-1][cx][cy-1];
			}
			else ans+=dp[i-1][cx][cy];
		}
		if(bit[i]==4)      cx--;
		else if(bit[i]==7) cy--;
		if(cx<0||cy<0) break;
	}
	return ans;
}
LL cal(LL n)
{
	int len=1;
	for(;;len++) if(dp[len][x][y]>=n) break;
	LL ans=0;
	int cx=x,cy=y;
	per(i,len,1)
	{
		rep(j,0,9)
		{
			LL sub;
			if(j==4)
			{
				if(cx) sub=dp[i-1][cx-1][cy];
				else continue;
			}
			else if(j==7)
			{
				if(cy) sub=dp[i-1][cx][cy-1];
				else continue;
			}
			else sub=dp[i-1][cx][cy];
			if(n-sub<=0)
			{
				if(j==4) cx--;
				if(j==7) cy--;
				ans=ans*10+j;
				break;
			}
			n-=sub;
		}
	}
	return ans;
}
/////////////////////////////////////////////////
void solve()
{
	predoing();
    rep(cases,1,read())
    {
    	printf("Case #%d:\n",cases);
    	l=read(); r=read(); x=read(); y=read(); l++;
    	LL rr=get_count(r),ll=get_count(l-1);
    	rep(n,1,read())
    	{
    		LL k=read();
    		if(k>rr-ll){puts("Nya!");continue;}
    		printf("%I64d\n",cal(k+ll));
    	}
    }
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    solve();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值