Cattle off more school seventh H Pair digit understand dp

Pair

The meaning of problems

Given ABC, Q value of x [1, A] and y values [1, B] pair group number exists <x, y> one kind of the following minimum conditions, \ (x \ y &> C \) , \ (X \) XOR \ (Y <C \)

analysis

Operations related to the binary bit computing is certainly and to think of and related to the position, we can consider enumerate each and every count, but so will the complexity of the explosion, there is nothing to enumerate every thought? Digital dp, we can consider the subject conditions containerization, The Complete Works of good demand, then seek his complement, to do everything \ (x \ & y <= c \) and \ (the X-\) xor \ (the y-> = c \ ) , is then subtracted by the answer corpus a × B.
Here digital state dp dp [bits] [Enumeration bound on A] [B enumerate the bound] [satisfies x and y <c] [satisfies x xor y> c] [A whether or not to take a number 0] [B taken whether or not the number 0]
there is a critical point, the state [satisfy x and y <c] [satisfies x xor y> c] with equality is not, why not take the like no, it is because if the condition is greater than the opposite when we will directly continue, then the state on the left of the current position is equal to c, and c is not equal to the corresponding conditions, which are two different states, but are legal so be recorded.
Beginning the writing of the status of DP [bits] [satisfies x and y <c] [satisfies x xor y> c] [A whether or not to take a number of 0] [B taken whether or not the 0 number], i.e. the two limit state is not put inside, the T. Because of the wording of conventional digital dp is evaluated between the two sections, so dp status to two the number of GM, plus if the limit, then, for our state, by definition, a state with two different definitions of there are actually several different, but did not limit restriction conditions majority, so before you write digit dp title states are no circumstances limit == 1, these cases are solved violence, of course, be the subject of these digits dp plus limit in the state, but so will every time memset dp array before seeking. In this question, since the operation only once, and are 01 strings, the case of two limit == 1 is very much, if you do not record these two states, it would lead to overtime T_T
first section AC Two state less T

#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
typedef long long ll;
using namespace std;
const int maxn=5e5+5;
int abit[35],bbit[35],cbit[35];
const int mod=1e9+7;
int n,q;
ll dp[35][3][3][3][3][3][3];
ll dfs(int dep,bool limit1,bool limit2,bool ok1,bool ok2,bool ling1,bool ling2){
    if(dep==0){
        return ling1&&ling2;
    }
    if(dp[dep][ok1][ok2][ling1][ling2][limit1][limit2]!=-1)return dp[dep][ok1][ok2][ling1][ling2][limit1][limit2];
    ll ans=0;
    int up1=limit1?abit[dep]:1;
     int up2=limit2?bbit[dep]:1;
    for(int i=0;i<=up1;i++){
        for(int j=0;j<=up2;j++){
            if(!ok1&&(i&j)>cbit[dep])continue;
            if(!ok2&&(i^j)<cbit[dep])continue;
            ans+=dfs(dep-1,limit1&&i==up1,limit2&&j==up2,ok1||((i&j)<cbit[dep]),ok2||((i^j)>cbit[dep]),ling1||i!=0,ling2||j!=0);
        }
    }
    return dp[dep][ok1][ok2][ling1][ling2][limit1][limit2]=ans;
}
ll solve(int a,int b,int c){
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=30;i++){
        abit[i]=a&1;
        bbit[i]=b&1;
        cbit[i]=c&1;
        a>>=1;
        b>>=1;
        c>>=1;
    }
    return dfs(30,1,1,0,0,0,0);
}
 
int main(){
    int t;
    scanf("%d",&t);
    int a,b,c;
    while(t--){
        scanf("%d%d%d",&a,&b,&c);
        printf("%lld\n",1ll*a*b-solve(a,b,c));
     
    }
    return 0;
}
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
typedef long long ll;
using namespace std;
const int maxn=5e5+5;
int abit[35],bbit[35],cbit[35];
const int mod=1e9+7;
int n,q;
ll dp[35][3][3][3][3];
ll dfs(int dep,bool limit1,bool limit2,bool ok1,bool ok2,bool ling1,bool ling2){
    if(dep==0){
        return ling1&&ling2;
    }
    if(!limit1&&!limit2&&dp[dep][ok1][ok2][ling1][ling2]!=-1)return dp[dep][ok1][ok2][ling1][ling2];
    ll ans=0;
    int up1=limit1?abit[dep]:1;
     int up2=limit2?bbit[dep]:1;
    for(int i=0;i<=up1;i++){
        for(int j=0;j<=up2;j++){
            if(!ok1&&(i&j)>cbit[dep])continue;
            if(!ok2&&(i^j)<cbit[dep])continue;
            ans+=dfs(dep-1,limit1&&i==up1,limit2&&j==up2,ok1||((i&j)<cbit[dep]),ok2||((i^j)>cbit[dep]),ling1||i!=0,ling2||j!=0);
        }
    }
    return dp[dep][ok1][ok2][ling1][ling2]=ans;
}
ll solve(int a,int b,int c){
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=30;i++){
        abit[i]=a&1;
        bbit[i]=b&1;
        cbit[i]=c&1;
        a>>=1;
        b>>=1;
        c>>=1;
    }
    return dfs(30,1,1,0,0,0,0);
}
 
int main(){
    int t;
    scanf("%d",&t);
    int a,b,c;
    while(t--){
        scanf("%d%d%d",&a,&b,&c);
        printf("%lld\n",1ll*a*b-solve(a,b,c));
     
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ttttttttrx/p/11420992.html