HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
\(Description\)
求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\)。
\(Solution\)
数位DP。
至于怎么求LIS,因为只有10个数,所以可以参照O(nlogn)求LIS的方法,状压记录状态。
每次加一个数和求LIS一样更新状态。最后状态中1的个数就是LIS的长度。
//93MS 3004K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=(1<<10)+3;
int K,bit[21];
LL f[21][N][12];//f[i][j][k] 当前到i位 状态为j 题目中的K为k(这个可以留着记忆化)
bool vis[21][N][12];
inline LL read()
{
LL now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int Count(int x)//可以预处理。。
{
int res=0;
for(; x; res+=x&1,x>>=1);
return res;
}
inline int Upd(int s,int x)//替换掉第一个>=x的位
{
for(int i=x; i<10; ++i)
if(s>>i&1) return (s^(1<<i))|(1<<x);
return s|(1<<x);
}
LL DFS(int x,bool lim,bool lead,int s)
{
if(!x) return Count(s)==K;
if(!lim && vis[x][s][K]) return f[x][s][K];//就算有前导零也是可以直接返回啊(本题s状态没变就没影响)
LL res=0; int up=lim?bit[x]:9;
for(int i=0; i<=up; ++i)
res+=DFS(x-1,lim&&i==up,lead&&!i,(lead&&!i)?0:Upd(s,i));
if(!lim) vis[x][s][K]=1, f[x][s][K]=res;
return res;
}
LL Calc(LL x)
{
int cnt=0;
for(; x; x/=10) bit[++cnt]=x%10;
if(cnt<K) return 0;
return DFS(cnt,1,1,0);
}
inline LL Solve()
{
LL l=read(),r=read(); K=read();
return Calc(r)-Calc(l-1);
}
int main()
{
for(int i=1,T=read(); i<=T; printf("Case #%d: %lld\n",i++,Solve()));
return 0;
}
HDU.4352.XHXJ's LIS(数位DP 状压 LIS)的更多相关文章
- hdu 4352 XHXJ's LIS(数位dp+状压)
Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefull ...
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- 【数位dp+状压】XHXJ 's LIS
题目 define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the enti ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- CodeForces1073E 数位dp+状压dp
http://codeforces.com/problemset/problem/1073/E 题意 给定K,L,R,求L~R之间最多不包含超过K个数码的数的和. 显然这是一道数位dp,在做的过程中会 ...
- lightoj 1021 - Painful Bases(数位dp+状压)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...
- HDU 4336 Card Collector:期望dp + 状压
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 一共有n种卡片.每买一袋零食,有可能赠送一张卡片,也可能没有. 每一种卡片赠送的概率为p ...
随机推荐
- OpenCV:Debug和Release模式 && 静态和动态编译
1.Release和Debug的区别 Release版称为发行版,Debug版称为调试版. Debug中可以单步执行.跟踪等功能,但生成的可执行文件比较大,代码运行速度较慢.Release版运行速度较 ...
- tomcat jetty
背景 其实这个比较毫无意义.只是当时突然知道了jetty,所以记下来,对于开发人员来说选择哪个一般意义不大. 转载 从架构上来看 Tomcat 在处理少数非常繁忙的连接上更有优势,也就是说连接的生命周 ...
- css 悬浮框
<style> .xfk { display: block; position: fixed; top: 150px; lef ...
- Python-HTML CSS题目
一.简答1.手写html模板,并解释模板每个标签的作用 <!doctype html> 文件类型html <html>页面根 <head>后勤内容 <meta ...
- CCF2015122消除类游戏(C语言版)
问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消 ...
- LeetCode(29): 两数相除
Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...
- java.lang.Math
四舍五人 System.out.println(Math.round(1.8f));//输出位2 static(静态)方法random() //: object/Shifting.java packa ...
- gcd,lcm
定理:gcd(a,b)*lcm(a,b)=a*b; 更相损减术:gcd(a,b)=gcd(b,a-b)=gcd(a,a-b) 欧几里得算法:gcd(a,b)=gcd(b,a mod b) 复杂度O(l ...
- hdu1890 splay维护区间翻转
这题的建模有点不太一样,是按结点横坐标赋予键值的 同时每次rotate和splay时都要注意下往上往下更新 /* 先建立好splay tree,将结点按num/输入顺序排序,遍历时每次将当前结点提到根 ...
- 持续集成一:git上传代码
先注册一个账号,注册地址:https://github.com/ 记住地址 下载git本地客户端,下载地址:https://git-scm.com/download/win 一路next傻瓜安装,加入 ...