【HDU】P5471 Count the Grid

Shaped pressure receiving repellent dp +

Since each has set n \ (2 ^ n \) th intersection, so we can put the entire sub-divided into a number of rectangular matrix comprising a plurality of intersection matrix.

Like the first sample can be so divided.

In this case, we do not discuss the case in many matrix intersect, the problem will simplify a lot.

Set \ (MX [i] \) represents the maximum value within a sub-matrix of i, \ (S [i] \) represents the number of i-th element contained within the matrix

Then, we consider shaped pressure dp. For a sub-matrix, we have the following decisions:

1. The maximum value of the sub-matrix is less than $ mx [i] $, this time as the program number \ ((MX [I] -1) ^ {S [I]} \) .
Since the sub- matrix of many small intersection matrix, as long as a small matrix reached the maximum \ (MX [i] \) , then the current sub actual maximum matrix also \ (MX [i] \) , is legitimate.

2. The maximum value within the sub-matrix \ (MX [I] \) , this time the program number \ (mx [i] ^ { S [i]} - (mx [i] -1) ^ {S [i ]} \) . Since the sub- matrix of many small intersection matrix, so to get the maximum current may affect a number of other small matrix.

Then, \ (DP [i] [j] \) represents the i-th former sub- matrices, to obtain maximum small matrix state j is a number of programs

状态转移:
\[ dp[i][j]=dp[i][j]+dp[i-1][j] \cdot (mx[i]-1)^{S[i]} \]

\[ dp[i][j|can[i]]=dp[i][j|can[i]]+dp[i-1][j]\cdot mx[i]^{S[i]}-(mx[i]-1)^{S[i]} \]

Wherein \ (CAN [i] \) represents the i-th sub- matrix to take maximum \ (MX [i] \) will be such that which a small matrix of maximum

For divided into many sub-matrices we can use the pre-wave of inclusion and exclusion.

Note: All small matrix and may not be set for the entire matrix, that is to say, in this case, there are several points that can be taken lightly (like sample 1) , we finally, multiplied by \ (m ^ CNT} {\) ( \ (CNT \) represents the number of points can be easily taken)

Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,h,w,m,n,dp[(1<<10)+5][(1<<10)+5],MM[(1<<10)+5],cas,sum=0;
const int MOD=1e9+7;
struct Set{
    int x1,y1,x2,y2,mx,SS;
    int S(){
        return max(x2-x1+1,0LL)*max(y2-y1+1,0LL);
    }
}A[10010],B[10010];
Set operator + (Set X,Set Y){
    Set Ans;
    Ans.mx=min(X.mx,Y.mx);
    Ans.x1=max(X.x1,Y.x1);
    Ans.y1=max(X.y1,Y.y1);
    Ans.x2=min(X.x2,Y.x2);
    Ans.y2=min(X.y2,Y.y2);
    return Ans;
}
int Quick_Pow(int a,int p){
    int res=1;
    while(p){
        if(p&1)res=res*a%MOD;
        a=a*a%MOD;
        p>>=1;
    }
    return res;
}
signed main(){
    scanf("%lld",&T);
    while(T--){
        memset(dp,0,sizeof(dp));
        memset(A,0,sizeof(A));
        memset(B,0,sizeof(B));
        memset(MM,0,sizeof(MM));
        scanf("%lld %lld %lld %lld",&h,&w,&m,&n);
        A[0]=Set{1,1,h,w,2e9+7,0};
        for(int i=1;i<=n;i++){
            scanf("%lld %lld %lld %lld %lld",&B[(1<<(i-1))].x1,&B[(1<<(i-1))].y1,&B[(1<<(i-1))].x2,&B[(1<<(i-1))].y2,&B[(1<<(i-1))].mx);
        }
        for(int i=1;i<(1<<n);i++)A[i]=A[i-(i&-i)]+B[i&-i];
        for(int i=(1<<n)-1;i>=0;i--){
            A[i].SS=A[i].S();
            for(int j=i+1;j<(1<<n);j++){
                if((j|i)==j)A[i].SS-=A[j].SS;
            }
            for(int j=1;j<=n;j++){
                if((1<<(j-1))&i){
                    if(A[i].mx==A[(1<<(j-1))].mx){
                        MM[i]|=(1<<(j-1));
                    }
                }
            }
        }
        dp[0][0]=1;
        for(int i=1;i<(1<<n);i++){
            for(int j=0;j<(1<<n);j++){
                dp[i][j]+=dp[i-1][j]*Quick_Pow(A[i].mx-1,A[i].SS)%MOD,dp[i][j]%=MOD;
                dp[i][j|MM[i]]+=dp[i-1][j]*(Quick_Pow(A[i].mx,A[i].SS)-Quick_Pow(A[i].mx-1,A[i].SS)+MOD)%MOD,dp[i][j|MM[i]]%=MOD;
            }
        }
        sum=h*w;
        for(int i=1;i<(1<<n);i++)sum-=A[i].SS;
        printf("Case #%lld: %lld\n",++cas,dp[(1<<n)-1][(1<<n)-1]*Quick_Pow(m,sum)%MOD);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/SillyTieT/p/11457530.html