hdu 4352

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352


题意:跳过废话看最后一段

Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of a number are strictly increasing. If you consider the number as a string and can get a longest strictly increasing subsequence the length of which is equal to k, the power of this number is k.. It is very simple to determine a single number’s power, but is it also easy to solve this problem with the numbers within an interval? xhxj has a little tired,she want a god cattle to help her solve this problem,the problem is: Determine how many numbers have the power value k in [L,R] in O(1)time.

求编号L,R范围内,有多少个编号,数字的最长上升子序列长度是k的

举例: 编号123654897,最长上升子序列是123489,长度是5。


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef __int64 ll;

int num[20],k;
ll dp[20][1<<10][15];//dp[len][s][k],len是数字的长度,s是用二进制表示的0~9组成的子序列,k是s的子序列长度

int getnew(int x,int s)//更新新的状态
{
	for(int i = x; i < 10; i++)
	{
		if(s&(1<<i))
			return (s^(1<<i))|(1<<x);//这个参考LIS的nlogn写法
	}
	return s|(1<<x);
}

int getcnt(int s)//返回集合s组成的子序列长度
{
	int cnt = 0;
	for(int i = 0; i < 10; i++)
	{
		if(s&(1<<i))
			cnt++;
	}
	return cnt;
}

ll dfs(int len,int s,bool zero,bool fp)//集合s,zero表示是上一个是不是0
{
	if(!len)
	{
		return getcnt(s)==k;
	}
	int fpmax = fp ? num[len] : 9;
	ll ret = 0;
	int tmp;
	if(!fp && dp[len][s][k]!=-1)
		return dp[len][s][k];

	for(int i = 0; i <= fpmax; i++)
	{
		if(zero&&i==0)
			tmp = 0;
		else tmp = getnew(i,s);
		ret += dfs(len-1,tmp,zero&&i==0,fp&&i==fpmax);
	}
	if(!fp)
		dp[len][s][k] = ret;
	return ret;
}

ll calc(ll x)
{
	int len = 0;

	while(x)
	{
		num[++len] = x%10;
		x/=10;
	}
	return dfs(len,0,true,true);
}

int main()
{
	int i,test;
	ll l,r;

	memset(dp,-1,sizeof(dp));
	scanf("%d",&test);
	i = 1;
	while(test--)
	{
		scanf("%I64d %I64d %d",&l,&r,&k);
		printf("Case #%d: ",i++);
		printf("%I64d\n",calc(r)-calc(l-1));
	}
}













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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值