• [hdu4301]DP


    题意:给一个2*n的矩形块,求把它分成k个连通块的方法数。(有公共边即视为联通)

    思路:由于宽度只有2,于是很容易设计状态使问题满足阶段性以及无后效性。具体来说,令dp[i][j][0]和dp[i][j][1]表示把前i行分成j个连通块最后两个格子分别属于和不属于同一个连通块的方法数,于是有下面的状态转移方程:

    dp[i][j][0]=dp[i-1][j-1][0..1]+dp[i-1][j][0]+dp[i-1][j][1]*2

    dp[i][j][1]=dp[i-1][j-2][0..1]+dp[i-1][j][1]+dp[i-1][j-1][0..1]*2

     1 #pragma comment(linker, "/STACK:10240000,10240000")
     2 
     3 #include <iostream>
     4 #include <cstdio>
     5 #include <algorithm>
     6 #include <cstdlib>
     7 #include <cstring>
     8 #include <map>
     9 #include <queue>
    10 #include <deque>
    11 #include <cmath>
    12 #include <vector>
    13 #include <ctime>
    14 #include <cctype>
    15 #include <set>
    16 #include <bitset>
    17 #include <functional>
    18 #include <numeric>
    19 #include <stdexcept>
    20 #include <utility>
    21 
    22 using namespace std;
    23 
    24 #define mem0(a) memset(a, 0, sizeof(a))
    25 #define mem_1(a) memset(a, -1, sizeof(a))
    26 #define lson l, m, rt << 1
    27 #define rson m + 1, r, rt << 1 | 1
    28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
    29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
    30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
    31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
    32 #define all(a) (a).begin(), (a).end()
    33 #define lowbit(x) ((x) & (-(x)))
    34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
    35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
    36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
    37 #define pchr(a) putchar(a)
    38 #define pstr(a) printf("%s", a)
    39 #define sstr(a) scanf("%s", a)
    40 #define sint(a) scanf("%d", &a)
    41 #define sint2(a, b) scanf("%d%d", &a, &b)
    42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
    43 #define pint(a) printf("%d
    ", a)
    44 #define test_print1(a) cout << "var1 = " << a << endl
    45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
    46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
    47 #define mp(a, b) make_pair(a, b)
    48 #define pb(a) push_back(a)
    49 
    50 typedef unsigned int uint;
    51 typedef long long LL;
    52 typedef pair<int, int> pii;
    53 typedef vector<int> vi;
    54 
    55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
    56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
    57 const int maxn = 1e3 + 7;
    58 const int md = 100000007;
    59 const int inf = 1e9 + 7;
    60 const LL inf_L = 1e18 + 7;
    61 const double pi = acos(-1.0);
    62 const double eps = 1e-6;
    63 
    64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
    65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
    66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
    67 template<class T>T condition(bool f, T a, T b){return f?a:b;}
    68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
    69 int make_id(int x, int y, int n) { return x * n + y; }
    70 
    71 int dp[1005][2010][2];
    72 
    73 void init() {
    74     dp[1][1][0] = 1;
    75     dp[1][2][1] = 1;
    76     for (int i = 2; i <= 1000; i ++) {
    77         rep_up1(j, 2 * i) {
    78             dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1] * 2 + dp[i - 1][j - 1][1] + dp[i - 1][j - 1][0];
    79             dp[i][j][1] = dp[i - 1][j][1] + dp[i - 1][j - 1][0] * 2 + dp[i - 1][j - 1][1] * 2;
    80             if (j > 1) dp[i][j][1] += dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1];
    81             dp[i][j][0] %= md;
    82             dp[i][j][1] %= md;
    83         }
    84     }
    85 }
    86 
    87 int main() {
    88     //freopen("in.txt", "r", stdin);
    89     init();
    90     int T;
    91     cin >> T;
    92     rep_up0(cas, T) {
    93         int n, k;
    94         cin >> n >> k;
    95         cout << (dp[n][k][0] + dp[n][k][1]) % md << endl;
    96     }
    97     return 0;
    98 }
    View Code
  • 相关阅读:
    数据结构之 移位操作
    大话设计模式之外观模式
    JSP的内置对象(application)
    从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
    linux线程应用
    【网络挖掘:成就与未来方向】之网络挖掘应用程序与相关概念
    Thinking in Java之匿名内部类
    [Go] map
    [跟着hsp步步学习系统]oracle培训学习集锦全360度扫描(2)
    HDU3791:二叉搜索树
  • 原文地址:https://www.cnblogs.com/jklongint/p/4523288.html
Copyright © 2020-2023  润新知