Valley Numer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
众所周知,度度熊非常喜欢数字。
它最近发明了一种新的数字:Valley Number,像山谷一样的数字。
![](../../../data/images/C777-1005-1.jpg)
当一个数字,从左到右依次看过去数字没有出现先递增接着递减的“山峰”现象,就被称作 Valley Number。它可以递增,也可以递减,还可以先递减再递增。在递增或递减的过程中可以出现相等的情况。
比如,1,10,12,212,32122都是 Valley Number。
121,12331,21212则不是。
度度熊想知道不大于N的Valley Number数有多少。
注意,前导0是不合法的。
它最近发明了一种新的数字:Valley Number,像山谷一样的数字。
![](../../../data/images/C777-1005-1.jpg)
当一个数字,从左到右依次看过去数字没有出现先递增接着递减的“山峰”现象,就被称作 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
每组数据包含一个数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
Recommend
题意:恩。。。开始我有点懵,因为题目说的有点不太清楚,然后我看了下样例就懂了。。数字不能出现有递增再递减的部分,然后问你有多少个满足条件的数。
思路:这读懂后就是裸的数位dp嘛。。dp[i][j][k]表示第i位前一个数是j状态为k的数有多少。状态有两种,0表示当前非递增状态,1表示当前递增状态。。代码如下:
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<utility>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define maxn 205
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-5;
const int mod = 1e9+7;
const LL INF = 0x3f3f3f3f;
LL dp[maxn][15][5];
LL p[maxn];
int num[maxn];
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
LL dfs(int pos, int status,int pre,int limit){
if (pos < 1)
return 1;
if (!limit&&~dp[pos][pre][status])
return dp[pos][pre][status];
int end = limit ? num[pos] : 9;
LL ans = 0;
if (status){
for (int i = pre; i <= end; i++){
ans += dfs(pos - 1, status, i, limit&&i == end);
ans %= mod;
}
}
else{
for (int i = 0; i <= end; i++){
if (i > pre){
ans += dfs(pos - 1, 1, i, limit&&i == end);
ans %= mod;
}
else{
if (!i&&pre == 10)
ans += dfs(pos - 1, 0, 10, limit&&i == end);
else
ans += dfs(pos - 1, 0, i, limit&&i == end);
ans %= mod;
}
}
}
if (!limit)
dp[pos][pre][status] = ans;
return ans;
}
LL solve(char s[]){
int len = strlen(s);
for (int i = 0; i < len; i++){
num[len - i] = s[i] - '0';
}
return dfs(len, 0, 10, 1);
}
int main(){
char s[maxn];
int t;
memset(dp, -1, sizeof(dp));
scanf("%d", &t);
while (t--){
scanf("%s", s);
printf("%lld\n", solve(s) - 1);
}
}