Digital DP_ HDU 4734

This question means that each number has a corresponding weight. For example, a number a has n digits, then its weight is F(a) =   A * 2 n-1  + A n-1  * 2 n -2  + ... + A 2  * 2 + A 1  * 1 , where A i represents the ith digit of number a, then given two numbers ab, find out how many numbers in the interval [0,b] satisfy : F(x)>= F(a)

Seeing this question, we can also think that the size of the weight has nothing to do with the size of the number, but is related to the number on the digit, and solves the problem of the number of establishments in the interval, then it can be thought of as an interval DP problem

But if we record the state: dp[i][j]: The current position is i, and now the weight of the first i bit is already j, the number of the following conditions is satisfied, if the state is defined in this way, then each The dp array needs to be updated for each input, because the F(a) given each time is different, that is, when F(a) is different, the value of dp[2][50] is only for the current calculation. F(a) is valid

If it is mentioned above, then we define dp[i][j][val]: when the current position is i, F(a) = val, and the weight of the previous i is already j, the following conditions are met. The number, in this case, is essentially the same as our situation above, but at the expense of space, the time has indeed been improved, and the maximum val in this question is about 2600, so the space is OK, but in other There may be more than this in the title of val, so this may also exceed the memory limit

So let's take a look at the solution: neither space nor time will be sacrificed

We know that no matter what the value of F(a) is, for example, if the weight of the first i bits is known to be x, then the maximum reachable weight of the latter bits has been determined, that is, F(a) - x , then when this position is reached again, if the later weights reach at most F(a) - x again, we can use the previous state, so we can use    dp[i][j]: the current position is i , When the maximum weight that can be used later is j, the number of satisfied conditions 

Then we have determined the representation of the state, then the rest is probably the template question of the digital DP

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <math.h>
#include <cmath>
#define INF 0x3f3f3f3f

using namespace std;
const int MAXN = 5000;
int dp[10][MAXN];
int a[10];
int F(int x)
{
    if(x == 0) return 0;
    int years = F(x/10);
    return ans * 2 + x % 10;
}
int all;
int dfs(int pos,int sum,bool limit)
{
    if(pos < 0) return sum <= all;
    if(sum > all) return 0;
    if(!limit && dp[pos][all - sum] != -1) return dp[pos][all-sum];
    int up = limit?a[pos]:9;
    int years = 0;
    for(int i = 0;i <= up;i ++)
        ans += dfs(pos-1,sum + i * (1<<pos), limit && i == a[pos]);
    if(!limit) dp[pos][all - sum]  = ans;
    return ans;
}
int Solve(int x)
{
    int pos = 0;
    while(x)
    {
        a[pos ++] = x % 10;
        x /= 10;
    }
    return dfs(pos-1 , 0, true);
}
intmain()
{
    int T,a,r;
    scanf("%d",&T);
    int case = 1;
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        scanf("%d%d",&a,&r);
        all = F(a);
        printf("Case #%d: %d\n",cas++,Solve(r));
    }

}

Reference blog

https://blog.csdn.net/wust_zzwh/article/details/52100392

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326608916&siteId=291194637