Valley Numer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1402    Accepted Submission(s): 713

Problem Description
众所周知,度度熊非常喜欢数字。

它最近发明了一种新的数字:Valley Number,像山谷一样的数字。

当一个数字,从左到右依次看过去数字没有出现先递增接着递减的“山峰”现象,就被称作 Valley Number。它可以递增,也可以递减,还可以先递减再递增。在递增或递减的过程中可以出现相等的情况。

比如,1,10,12,212,32122都是 Valley Number。

121,12331,21212则不是。

度度熊想知道不大于N的Valley Number数有多少。

注意,前导0是不合法的。

 
Input
第一行为T,表示输入数据组数。

每组数据包含一个数N。

● 1≤T≤200

● 1≤length(N)≤100

 
Output
对每组数据输出不大于N的Valley Number个数,结果对 1 000 000 007 取模。
 
Sample Input
3
3
14
120
 
Sample Output
3
14
119
 
Source
 
思路:这应该算一道比较简单的数位dp题,数位dp入门题,刚学数位dp,还不是很熟,敲这题时,忘了考虑前导0的情况,参考了博客才知道自己错哪了。
定义一个dp[i][j][k],i表示位置,j表示前一位,k表示单调性,0表示未知,1表示上升,2表示下降。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<cmath>
#include<list>
#include<deque>
#include<cstdlib>
#include<bitset>
#include<stack>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
string s;
ll a[],dp[][][];
ll dfs(int pos,int pre,int turn,bool limit,bool invalid)
{//pos为位置,turn为1表示上升,2下降,0未知,limit记录数位是否达上界,invalid记录前导是否都为0
if(pos==-) return invalid?:; //如果全部为0,不合法返回0,否则返回1
if(!limit&&dp[pos][pre][turn]!=-)
return dp[pos][pre][turn]; //被记忆过了
int up=limit?a[pos]:; //是否达上界
ll ans=;
for(int i=;i<=up;i++)
{
if(turn==&&i<pre) continue;
int x;
if(i==pre) x=turn;
else if(i>pre) x=;
else x=;
if(invalid) x=;
ans=(ans+dfs(pos-,i,x,limit&&i==a[pos],i==&&invalid))%mod;
}
if(!limit) dp[pos][pre][turn]=ans; //记忆化
return ans;
}
ll solve(string x)
{
int pos=;
for(int i=x.length()-;i>=;i--)
a[pos++]=x[i]-'';
return dfs(pos-,,,true,true);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie();
int T;
cin>>T;
while(T--)
{
string s;
memset(dp,-,sizeof(dp));
cin>>s;
cout<<solve(s)<<endl;
}
return ;
}

hdu6148 百度之星程序设计竞赛复赛 (数位dp)的更多相关文章

  1. 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】

    Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. 2017"百度之星"程序设计大赛 - 复赛 01,03,05

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. 2017"百度之星"程序设计大赛 - 复赛1003&&HDU 6146 Pokémon GO【数学,递推,dp】

    Pokémon GO Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 2017"百度之星"程序设计大赛 - 复赛1001&&HDU 6144 Arithmetic of Bomb【java大模拟】

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. [SinGuLaRiTy] 2017 百度之星程序设计大赛 复赛

    [SinGuLaRiTy-1038] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Arithmetic of Bomb Problem D ...

  6. 2017百度之星程序设计大赛 - 复赛 Arithmetic of Bomb

    http://acm.hdu.edu.cn/showproblem.php?pid=6144 解法:一个简单的模拟 #include <bits/stdc++.h> using names ...

  7. 2017"百度之星"程序设计大赛 - 复赛

    Arithmetic of Bomb  Accepts: 1050  Submissions: 1762  Time Limit: 2000/1000 MS (Java/Others)  Memory ...

  8. 【2017百度之星程序设计大赛 - 复赛】Valley Numer

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6148 [题意] 在这里写题意 [题解] 先把1..N里面的山峰数字个数算出来->x 然后用N减去这 ...

  9. 【2017"百度之星"程序设计大赛 - 复赛】Arithmetic of Bomb

    [链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=777&pid=1001 [题意] 在这里写 [题解] ...

随机推荐

  1. JQuery/JS select标签动态设置选中值、设置禁止选择 button按钮禁止点击 select获取选中值

    //**1.设置选中值:(根据索引确定选中值)**// var osel=document.getElementById("selID"); //得到select的ID var o ...

  2. [转帖]xserver相关知识汇总

    xserver相关知识汇总 https://blog.csdn.net/QTVLC/article/details/81739984   本文主要是从以下几个方面介绍xorg-xserver 相关的知 ...

  3. 《Effective C++》设计与声明:条款18-条款25

    条款18:让接口容易被正确使用,不容易被误用 注意使用const,explicit,shared_ptr等来限制接口. 必要时可以创建一些新的类型,限制类型操作,束缚对象等. 注意保持接口的一致性,且 ...

  4. easyUI 数据表格datagrid的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. 版本控制--git+idea

  6. Python时间的简单使用

    1.time.strptime(string[, format]),string -- 时间字符串.format -- 格式化字符串.返回struct_time对象.     把字符串转换为时间格式, ...

  7. linux中的set -e 与set -o pipefail

    1.set -e "Exit immediately if a simple command exits with a non-zero status." 在“set -e”之后出 ...

  8. saltstack一

    Saltstack概述 Salt一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, 使其可以 ...

  9. html5 表單屬性

    新的 form 属性: autocomplete novalidate 新的 input 属性: autocomplete autofocus form form overrides (formact ...

  10. Eclipse:报错Failed to read artifact descriptor for org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.

    导入SVN下载的MAVEN项目时springboot报错: pom.xml文件报错 Failed to read artifact descriptor for org.springframework ...