HDU6148-Valley Numer【数位dp】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39396954/article/details/82253457

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6148

Valley Numer

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


 

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

 

扫描二维码关注公众号,回复: 4133275 查看本文章

Output

对每组数据输出不大于N的Valley Number个数,结果对 1 000 000 007 取模。

 

Sample Input

 

3 3 14 120

 

Sample Output

 

3 14 119

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;

ll a[111],dp[111][10][3];
char s[111];

ll dfs(ll pos,ll pre,ll status,bool limit,bool lead)
{
	if(pos==-1)
	{
		return lead?0:1;
	}
	if(!limit&&!lead&&dp[pos][pre][status]!=-1)
	{
		return dp[pos][pre][status];
	}
	ll res=0;
	ll up=limit?a[pos]:9;
	for(ll i=0;i<=up;i++)
	{
		if(status&&i<pre)
			continue;
		ll nxt=0;
		if(!lead&&(i>pre)||status)
			nxt=1;
		ll nxtlead=0;
		if(i==0&&lead)
			nxtlead=1;
		ll nxtlimit=0;
		if(limit&&i==a[pos])
			nxtlimit=1;
		res=(res+dfs(pos-1,i,nxt,nxtlimit,nxtlead))%mod;
	}
	if(!limit&&!lead)
		dp[pos][pre][status]=res;
	return res;
}

int main()
{
	ll t;
	scanf("%lld",&t);
	while(t--)
	{
		memset(dp,-1,sizeof(dp));
		scanf("%s",s);
		ll l=strlen(s);
		for(ll i=0;i<l;i++)
		{
			a[i]=s[l-i-1]-'0';
		}
		printf("%lld\n",dfs(l-1,0,0,true,true));
	}
	return 0;
}

看到几个写数位dp很棒的博客:https://blog.csdn.net/brazy/article/details/77427699

https://www.cnblogs.com/agenthtb/p/7392518.html

猜你喜欢

转载自blog.csdn.net/qq_39396954/article/details/82253457