Digital dp (binary 01 issues)

Meaning of the questions: to give you a range, ask how many sections there are several conditions to meet. Condition is: the number into the binary 0 if the number is equal to the number greater than 1, the number of the conditions was met.

Solution: dp [Digital] [sta] pos pos bits represents the number 0 -1 pos number of digits preceding the sta, and the number of number satisfying the condition.

Because dfs sta process may be negative (array subscript can not be negative), but not given, but the final number meets the conditions, the initial value of 32 sta avoided negative. Because leading zeros will affect the outcome, so to Category talk.

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[40];
int dp[40][70];

int dfs(int pos , int sta , int lead , int limit)
{
    if(pos == -1) return sta >= 32;
    if(!limit && !lead && dp[pos][sta] != -1) return dp[pos][sta];
    int up = limit ? a[pos] : 1 ;
    int ans = 0 ;
    for(int i = 0 ; i <= up ; i++)
    {
        if(lead && i == 0) ans += dfs(pos-1 , sta , lead , limit && a[pos] == i);
        else ans += dfs(pos -1 , sta + (i == 0 ? 1 : -1) , false , limit && a[pos] == i);
    }
    if(!limit && !lead) dp[pos][sta] = ans ;
    return ans ;
}

int solve(int x)
{
    int pos = 0 ;
    while(x)
    {
        a[pos++] = x % 2 ;
        x /= 2 ;
    }

    return dfs(pos-1 , 32 , true , true);
}

int main()
{
    /*#ifdef ONLINE_JUDGE
    #else
        freopen("D:/c++/in.txt", "r", stdin);
        freopen("D:/c++/out.txt", "w", stdout);
    #endif*/
    int l , r ;
    memset(dp , -1 , sizeof(dp));
    scanf("%d%d" , &l , &r);
    printf("%d\n" , solve(r) - solve(l-1));





    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/12000308.html