题目链接[kuangbin带你飞]专题十五 数位DP B - XHXJ’s LIS

题意

给定区间。求出有多少个数满足最长上升子序列(将数看作字符串)的长度为k。

思路

一个数的上升子序列最大长度为10,所以每个上升子序列的状态都能够用10个二进制位来表示。

上升子序列的变化能够用LIS的方式来更新。

dp[len][num][k]

len为当前的位,num为当前上升子序列的状态。k表示子序列的长度。

next[s][num]为记录预处理的子序列的状态变化。

cnt [num]记录各个状态的最长上升子序列的长度。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector> using namespace std; #define LL long long LL dp[20][1<<10][11];
int dis[20];
int cnt[1<<10];
int nxt[10][1<<10]; int getnext(int num, int s)
{
for(int i=s; i<10; i++)
{
if(num & (1<<i))
return (num^(1<<i)) | 1<<s;
}
return num | 1<<s;
} LL dfs(int k, int len, int num, bool flag, bool zero)
{
if(len < 0)
return cnt[num] == k;
if(!flag && dp[len][num][k]!=-1)
return dp[len][num][k];
LL ans = 0;
int end = flag?dis[len]:9;
for(int i=0; i<=end; i++)
ans += dfs(k, len-1, (zero&&i==0)? num:nxt[i][num], flag&&i==end, zero&&i==0);
if(!flag)
dp[len][num][k] = ans;
return ans;
} LL solve(LL n, int k)
{
int pos = 0;
while(n)
{
dis[pos++] = n%10;
n /= 10;
}
return dfs(k, pos-1, 0, 1, 1);
} void init()
{
memset(dp, -1, sizeof(dp));
for(int i=0; i<1<<10; i++)
{
cnt[i] = 0;
for(int j=0; j<10; j++)
{
if(i & (1<<j))
cnt[i]++;
nxt[j][i] = getnext(i, j);
}
}
} int main()
{
int T;
scanf("%d", &T);
init();
for(int i=1; i<=T; i++)
{
long long l, r, k;
scanf("%lld%lld%lld", &l, &r, &k);
printf("Case #%d: %lld\n", i, solve(r, k)-solve(l-1, k));
}
return 0;
}

HDU 4352 XHXJ&#39;s LIS(数位dp&amp;状态压缩)的更多相关文章

  1. XHXJ's LIS HDU - 4352 最长递增序列&数位dp

    代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  3. HDU 4352 XHXJ's LIS 数位dp lis

    目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...

  4. HDU 4352 XHXJ's LIS HDU(数位DP)

    HDU 4352 XHXJ's LIS HDU 题目大意 给你L到R区间,和一个数字K,然后让你求L到R区间之内满足最长上升子序列长度为K的数字有多少个 solution 简洁明了的题意总是让人无从下 ...

  5. hdu 4352 XHXJ's LIS (数位dp+状态压缩)

    Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully readin ...

  6. 【状态压缩DP】HDU 4352 XHXJ'S LIS

    题目大意 Vjudge链接 定义一个数的内部LIS长度表示这个数每个数位构成的序列的LIS长度,给出区间\([l,r]\),求区间内内部LIS长度为\(k\)的数的个数. 输入格式 第一行给出数据组数 ...

  7. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  8. hdu 4352 XHXJ's LIS 数位dp+状态压缩

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...

  9. hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)

    传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...

随机推荐

  1. xxtea 文件加密与解密

    加密 cocos luacompile -s src -d dst_dir -e -b xxxxx -k xxxxx --disable-compile 解密 cocos luacompile -s ...

  2. JavaSE-13 内部类

    学习要点 内部类的定义 内部类的应用 内部类 定义 Java的一个类中包含着另一类. A类和B类是C类的外部类.B类是C类的外部类.A类也称为顶层类. 如何使用内部类 public class MyF ...

  3. sql 触发器 针对一张表数据写入 另一张表 的增删改

    ALTER TRIGGER [dbo].[tri_test2] ON [dbo].[student] for INSERT,DELETE,UPDATEAS BEGIN if not exists (s ...

  4. TCP的链接过程

    ** TCP 三次握手 四次挥手** 客户端 服务器端 交互的模式 ---> 应答模式* 应答模式 ---> 客户端发起请求 服务器端回应请求** 客户端先去发起请求 ---> 查看 ...

  5. egg.js上传文件到本地

    'use strict'; const Service = require('egg').Service; const fs = require('fs'); const path = require ...

  6. Python之数字

    Python之数字 int(数字)===>在Python3中,int没有范围,在Python2中,int超出范围就叫长整型(Long). 浮点运算:单精度 float 双精度 double a: ...

  7. python flask获取微信用户信息报404,nginx问题

    在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...

  8. Cropping multiple images the same way

    The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite), so make sure that yo ...

  9. windows窗口过程函数名词解析

    windows窗口过程函数名词解析 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) 1. LR ...

  10. private关键字

    Student.java /* * 学生类 * * 通过对象直接访问成员变量,会存在数据安全问题 * 这个时候,我们就想能不能不让外界对象直接访问成员变量呢? * 答案:能 * 如何实现呢? * pr ...