BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing
题意:求[l,r]非递增和非递减序列的个数
思路:数位dp,dp[pos][pre][status]
- pos:处理到第几位
- pre:前一位是什么
- status:是否有前导零
递增递减差不多思路,不过他们计算的过程中像5555,444 这样的重复串会多算,所以要剪掉。个数是(pos-1)*9+digit[最高位],比如一位重复子串是:1,2,3,4...9,9个,二位重复子串:11,22,33,44,...,99,9个;同理,其他类推;
不过这个题如果dp值每算完一个[l,r]就清零,会超时。那么我们这么分析,算[l1,r1],[l2,r2]这两个区间时,dp是否真的有必要清零呢,答案是否定的,记忆化搜索的过程中记录的dp值如果计算过,那么当其他值算到他时,这个值是可以用的。具体的自己想想就好了
/**************************************************************
Problem:BNUOJ 52325 Increasing or Decreasing
User: youmi
Language: C++
Result: Accepted
Time: 380 ms
Memory: 1632 KB
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%lld",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%lld\n",a)
#define rep(i,from,to) for(int i=from;i<=to;i++)
#define irep(i,to,from) for(int i=to;i>=from;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define eps 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl
const double pi=*atan(1.0); using namespace std;
typedef long long ll;
template <class T> inline void read(T &n)
{
char c; int flag = ;
for (c = getchar(); !(c >= '' && c <= '' || c == '-'); c = getchar()); if (c == '-') flag = -, n = ; else n = c - '';
for (c = getchar(); c >= '' && c <= ''; c = getchar()) n = n * + c - ''; n *= flag;
}
ll Pow(ll base, ll n, ll mo)
{
ll res=;
while(n)
{
if(n&)
res=res*base%mo;
n>>=;
base=base*base%mo;
}
return res;
}
//*************************** int n;
const int maxn=+;
const ll mod=;
int digit[];
ll dp0[][][];
ll dp1[][][];
int tot=;
ll dfs0(int pos,int pre,int status,int limit)
{
if(pos<)
return status;
if(!limit&&dp0[pos][pre][status]!=-)
return dp0[pos][pre][status];
int ed=limit?digit[pos]:;
ll res=;
if(status==)
{
for(int i=;i<=min(pre,ed);i++)
{
if(i==)
res+=dfs0(pos-,,,limit&&(i==ed));
else
res+=dfs0(pos-,i,,limit&&(i==ed));
}
}
else
{
for(int i=;i<=min(ed,pre);i++)
res+=dfs0(pos-,i,status,limit&&(i==ed));
}
if(!limit)
dp0[pos][pre][status]=res;
return res;
}
ll dfs1(int pos,int pre,int status,int limit)
{
if(pos<)
return status;
if(!limit&&dp1[pos][pre][status]!=-)
return dp1[pos][pre][status];
int ed=limit?digit[pos]:;
ll res=;
for(int i=pre;i<=ed;i++)
res+=dfs1(pos-,i,status||i,limit&&(i==ed));
if(!limit)
dp1[pos][pre][status]=res;
return res;
}
void work(ll num)
{
tot=;
while(num)
{
digit[tot++]=num%;
num/=;
}
}
ll solve(ll num)
{
if(num==)
return ;
ll ans=(tot-)*+digit[tot-];
ll temp=;
int tt=;
while(tt<tot)
temp=temp*+digit[tot-],tt++;
if(temp>num)
ans--;
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int T_T;
scanf("%d",&T_T);
ones(dp0);
ones(dp1);
for(int kase=;kase<=T_T;kase++)
{
ll num;
read(num);
num--;
work(num);
ll temp0=dfs0(tot-,,,);
temp0+=dfs1(tot-,,,);
temp0-=solve(num);
read(num);
work(num);
ll temp1=dfs0(tot-,,,);
temp1+=dfs1(tot-,,,);
temp1-=solve(num);
ptlld(temp1-temp0);
}
return ;
}
BNUOJ 52325 Increasing or Decreasing 数位dp的更多相关文章
- 【数位DP】bnuoj 52813 J. Deciphering Oracles
http://acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/J [AC] #include<bits/stdc++.h> usin ...
- Problem I. Increasing or Decreasing MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016
题面: Problem I. Increasing or DecreasingInput file: standard inputOutput file: standard outputTime li ...
- 浅谈数位DP
在了解数位dp之前,先来看一个问题: 例1.求a~b中不包含49的数的个数. 0 < a.b < 2*10^9 注意到n的数据范围非常大,暴力求解是不可能的,考虑dp,如果直接记录下数字, ...
- 【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)
SORTBIT - Sorted bit squence no tags Let's consider the 32 bit representation of all integers i from ...
- 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- HDU 4352 数位dp
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu4352 XHXJ's LIS(数位DP + LIS + 状态压缩)
#define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire ...
随机推荐
- 12款响应式的 jQuery 旋转木马(传送带)插件
在企业网站,作品集网站,电子商务网站或任何其他类型的网站内容显示图片可以使用 jQuery 旋转木马(传送带)插件来实现. jQuery 旋转木马插件允许开发人员以水平或垂直的方式显示内容,视频和图像 ...
- Frameless - 用于预览 iOS8 原型的浏览器
Frameless 是一个用于在 iOS8 中预览产品原型的浏览器.可以可以帮助那些需要一个简单的方法来预览 iOS 设备上的原型设计和开发效果.没有状态栏,通过手势控制浏览器的历史以及键盘的显示. ...
- 2013年最新流行的响应式 WordPress 主题【下篇】
在这篇文章中,我们收集了一些在2013年发布的最好的免费的响应式 WordPress 主题.这些主题包括高级功能,如自定义模板.自定义窗口小部件.自定义菜单主题选项等.让我们来看看下面的清单,并希望你 ...
- go语言 类型:布尔类型
Go语言中的布尔类型与其他语言基本一致,关键字也为bool,可赋值为预定义的true和false示例代码如下: var v1 bool v1 = true v2 := (1 == 2) // v2也会 ...
- HTML5拖放(drag and drop)与plupload的懒人上传
HTML5拖放能够将本地的文件拖放到页面上,plupload又是很好的文件上传插件,而今天就将两者结合,做了个文件拖拽上传的功能. 简述HTML5拖放 拖放是HTML5标准的一部分,任何元素都能够拖放 ...
- ae_将面积小于1500的Feature同附近Feature进行合并
private void 合并1500图斑ToolStripMenuItem_Click(object sender, EventArgs e) { /* *将图层中面积小于1500的图斑与之相同BS ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q19-Q20)
Question 19 You are designing a custom SharePoint 2010 solution package. It will include a feature t ...
- 虚拟机克隆以后出现“需要整合虚拟机磁盘”的解决方法
问题描述 在虚拟机克隆完毕以后,原始虚拟机提示"需要整合虚拟机磁盘" 在"任务与事件"栏中看到以下信息 解决方法 从上面可以看到是因为整合失败导致的,那么我们只 ...
- UISlider相关
设置slider当前位置的图像 [slider setThumbImage:[UIImage imageNamed:@"dd.png"] forState:UIControlSta ...
- 操作系统开发系列—13.h.延时操作
计数器的工作原理是这样的:它有一个输入频率,在PC上是1193180HZ.在每一个时钟周期(CLK cycle),计数器值会减1,当减到0时,就会触发一个输出.由于计数器是16位的,所以最大值是655 ...