E. Another Filling the Grid 状压dp

  http://codeforces.com/contest/1228/my

题意:有个nm的矩形  每个格子可以取1-k中的一种数字  问有多少种填法  使得每行每列至少都有一个1

题解:设置dp[i][j] 表示 当前处理到i行有j列为1的方案数   然后统计答案贡献即可   注意改行至少取一个1

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<‘=‘<<(x)<<endl)
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=300;
const ll mod=1e9+7;
ll dp[N][N],mi[N],mi_1[N],C[N][N],n,k;
int main()
{
    cin>>n>>k;
    rep(i,0,n)
    {
        C[i][0]=1;
        rep(j,1,i)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    mi[0]=mi_1[0]=1;
    rep(i,1,n)mi[i]=mi[i-1]*k%mod,mi_1[i]=mi_1[i-1]*(k-1)%mod;
    rep(i,1,n)dp[1][i]=C[n][i]*mi_1[n-i]%mod;
    rep(i,2,n)rep(j,0,n)rep(s,0,j)
    {
      dp[i][j]=(dp[i][j]+dp[i-1][s]*C[n-s][j-s]%mod*mi[s]%mod*mi_1[n-j]%mod)%mod;
      if(j==s)dp[i][j]=(dp[i][j]-mi_1[n]*dp[i-1][s]%mod+mod)%mod;//一定要容斥掉全部非一的方案数
    }
    cout<<dp[n][n];
    return 0;
}

原文地址:https://www.cnblogs.com/bxd123/p/11618291.html

时间: 2024-11-09 08:31:02

E. Another Filling the Grid 状压dp的相关文章

zoj zju 2994 Tiling a Grid With Dominoes 状压dp

Tiling a Grid With Dominoes Time Limit: 2 Seconds      Memory Limit: 65536 KB We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five differen

poj 2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12232   Accepted: 7142 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

POJ 题目2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 7876 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU 3681 Prison Break(bfs+二分+状压DP)

Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3778    Accepted Submission(s): 992 Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But on

poj2411--Mondriaan&#39;s Dream(状压dp+dfs)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12315   Accepted: 7189 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU-4856 Tunnels (BFS+状压DP)

Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of cur

HDOJ 5067 Harry And Dig Machine 状压DP

状压DP....dp[i][j]已经走过的点的状态,目前再j点的最小距离 Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 560    Accepted Submission(s): 210 Problem Description As we all know, Harry Porter le

ZOJ 3306 状压dp

转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds Memory Limit: 32768 KB In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of