您的位置:首页 > 其它

hdu 6148 数位DP(板子 递增递减

2017-08-19 22:40 267 查看
传送门


Valley Numer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 431    Accepted Submission(s): 227


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

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

 

Recommend

We have carefully selected several similar problems for you:  6160 6159 6158 6157 6156 

中文就不说了,简单的数位DP

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define me(x,y) memset(x,y,sizeof(x));
#define LL long long
const LL mod=1e9+7;
typedef long long ll;
int a[25];
int dp[200][10][4];
char s[200];
ll dfs(int pos,int pre,int u,int limit,int lead=1) //limit limit
{
if(!pos)return 1;
if(!limit&&dp[pos][pre][u]!=-1)return dp[pos][pre][u];
int en=limit?s[pos]-'0':9;ll ans=0;
for(int i=0;i<=en;i++)
{
if(lead)
{
ans=(ans+dfs(pos-1,i,0,limit&&i==en,lead&&i==0))%mod;
}else
{
if(u==0)
{
int U=0;
if(i>pre)U=2;
if(i<pre)U=1;
ans=(ans+dfs(pos-1,i,U,limit&&i==en,lead&&i==0))%mod;
}
if(u==1)
{
int U=u;
if(i>pre)U=3;
ans=(ans+dfs(pos-1,i,U,limit&&i==en,lead&&i==0))%mod;
}
if(u==2||u==3)
{
if(i<pre)continue;
ans=(ans+dfs(pos-1,i,u,limit&&i==en,lead&&i==0))%mod;
}
}
}
if(!limit)dp[pos][pre][u]=ans;
return ans;
}

int main()
{
int t;
me(dp,-1);
scanf("%d",&t);
while(t--)
{
scanf("%s",s+1);
int len=strlen(s+1);
reverse(s+1,s+1+len);
printf("%lld\n",dfs(len,0,0,1)-1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: