您的位置:首页 > 其它

2017百度之星复赛:1006. Valley Numer(数位DP)

2017-08-18 18:06 483 查看


Valley Numer

 
 Accepts: 548
 
 Submissions: 1125

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

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


数位dp,dp[len][pre][up]表示当前到了第len位,上一位是pre,是否出现过递增的情况个数

#include<stdio.h>
#include<string.h>
#define mod 1000000007
#define LL long long
char pp[105];
LL str[105], temp[105], dp[105][12][105];
LL Sech(LL pos, LL pre, LL up, LL flag, LL k)
{
LL u, i, ans;
if(pos<0)
return 1;
if(flag==0 && k==0 && dp[pos][pre][up]!=-1)
return dp[pos][pre][up];
if(flag==1)  u = str[pos];
else  u = 9;
ans = 0;
for(i=0;i<=u;i++)
{
temp[pos] = i;
if(k==1)
{
ans += Sech(pos-1, i, 0, flag && i==u, k && (i==0));
continue;
}
if(i==pre)
ans += Sech(pos-1, i, up, flag && i==u, 0);
else if(i<pre)
{
if(up>0)
continue;
ans += Sech(pos-1, i, up, flag && i==u, 0);
}
else if(i>pre)
ans += Sech(pos-1, i, up+1, flag && i==u, 0);
}
ans %= mod;
if(flag==0 && k==0)
dp[pos][pre][up] = ans;
return ans;
}
int main(void)
{
LL T, n, i;
scanf("%I64d", &T);
memset(dp, -1, sizeof(dp));
while(T--)
{
scanf("%s", pp+1);
n = strlen(pp+1);
for(i=n;i>=1;i--)
str[i-1] = pp[n-i+1]-'0';
printf("%I64d\n", ((Sech(n-1, 0, 0, 1, 1)-1+mod)%mod));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: