HDU 6148 Valley Numer

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

6148 Valley Numer 数位DP

题意:

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

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

121,12331,21212则不是。

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

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

思路:

很典型的数位DP,数据范围能达到100位,要求和数位挂钩。这里处理的一些细节包括:

前导0的处理,如果是最高位为0,那么我们用10代替0,这样就可以一起处理。

记录的状态包括是否递增和上一位数以及最重要的当前数位。

模板稍微修改,注意对DFS的搜索条件。

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 200+10;
const int mod =  1e9+7;
char num[maxn];
int l;
LL dp[maxn][12][2];        ///当前第i位数为j,且状态为k的数量,k=0,没有出现递增,k=1,出现过递增
LL dfs(int len,int nu,bool ifadd,bool limit)
{
    if(len==0) return 1ll;
    if(!limit && dp[len][nu][ifadd]) return dp[len][nu][ifadd];
    int upperBound = (limit?num[len]:9);
    LL sum=0;
    if (ifadd){
        for (int i = nu; i <= upperBound; i++){
            sum += dfs(len - 1,i, ifadd, limit&&i == upperBound);
            sum %= mod;
        }
    }
    else{
        for (int i = 0; i <= upperBound; i++){
                if (!i&&nu == 10)
                    sum += dfs(len - 1, 10,0, limit&&i == upperBound);
                else
                    sum += dfs(len - 1,i, i>nu, limit&&i == upperBound);
                sum %= mod;
        }
    }
    if(!limit) dp[len][nu][ifadd]=sum;
    return sum;
}
LL Solve()
{
    memset(dp,0,sizeof(dp));
    l = strlen(num+1);
    for(int i=1;i<=l;i++)
        num[i]-='0';
    for(int i=1;i<=l/2;i++){
        swap(num[i],num[l-i+1]);
    }
    return dfs(l,10,false,true);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int T;
    scanf("%d",&T);
    while(T--) {
        getchar();
        scanf("%s",num+1);
        printf("%lld\n",Solve()-1);
    }
    return 0;
}
阅读更多
换一批

Ice Valley

08-24

On the way to the Dragon's Castle, there is an Ice Valley. Now Lott wants to pass through the Ice Valley. While in the Ice Valley, there was a lot of treasure, and the treasure can be helpful to Lott, so Lott wants to get as many treasure as he can.nnNow, Lott has reached the Ice Valley, and he finds that the Ice Valley is made up of n*m blocks. And on the blocks there are different signs. Lott has been told by friendly NPC that the meaning of each signs:nnThe meaning of each signs:nnNothing: The block is an empty ground, you step on it and then you can choose to go up, down, left, right as you like. However the number of this kind of block is very few no more than 100.nArrow: The block has magic power, you step on it and you have to go to the next block in the same direction as the arrow.nWall: The block is a wall, and you can never step on it.nBox: The block has a box with treasure, you step on it and you can open the box to get the treasure, then, you can go up, down, left, right as you like. The number of the box in the valley is always no greater than 10.nHole: The block is a trap, and you will die if you step on it.nNow, Lott starts at the entrance of the valley x1, y1. He wants to get to the exit at the position of x2, y2. And at the same time, Lott wants to get the most treasure he can. The entrance and the exit are both signed nothing. The entrance and the exit can be the same positions.nnNow, we assume that each movement from one block to another costs 1 second and each opening of one box spends 2 seconds, and we want to know the minimum time Lott uses to pass through the valley with the most treasure.nnInputnnThere are multiple cases.nnIn each cases, the first line contains two integers n , m. (1<=n<=500 , 1<=m<=500)nnThen n lines with m characters describing the valley. And '0' represents nothing; 'L','U','D','R' represents arrow with the direction of left, up, down, right; 'W' represents a wall; '$' represents a box with treasure; '#' represents the trap hole.nnThen the last line with four integer x1, y1, x2, y2. (1<=x1<=500 , 1<=y1<=500 , 1<=x2<=500 , 1<=y2<=500)nnOutputnnOf each case, output one line with one integer representing the minimum time Lott will use, if he cannot pass through the valley, output -1.nnSample Inputnn5 5nDL0R$nDWDWDnDL$LLnD#DWUnRURR0n1 3 5 5n5 5n0RRRDnUW$#DnUD0UDnU#$WDnULLLLn1 1 3 3nSample Outputnn14n-1

没有更多推荐了,返回首页