题目链接: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));
}
}