LightOJ - 1246 - Colorful Board(DP)

链接:

https://vjudge.net/problem/LightOJ-1246

题意:

You are given a rectangular board. You are asked to draw M horizontal lines and N vertical lines in that board, so that the whole board will be divided into (M+1) x (N+1) cells. So, there will be M+1 rows each of which will exactly contain N+1 cells or columns. The yth cell of xth row can be called as cell(x, y). The distance between two cells is the summation of row difference and column difference of those two cells. So, the distance between cell(x1, y1) and cell(x2, y2) is

|x1 - x2| + |y1 - y2|

For example, the distance between cell (2, 3) and cell (3, 2) is |2 - 3| + |3 - 2| = 1 + 1 = 2.

After that you have to color every cell of the board. For that you are given K different colors. To make the board more beautiful you have to make sure that no two cells having the same color can have odd distance between them. For example, if you color cell (3, 5) with red, you cannot color cell (5, 8) with red, as the distance between them is 5, which is odd. Note that you can keep some color unused, but you can‘t keep some cell uncolored.

You have to determine how many ways to color the board using those K colors.

思路:

将图分为两个部分,任意一个部分的点到另一个部分的任意一个点的距离都是奇数。
这样就转变成了一个DP,Dp[i][j]表示n个位置,放了j种颜色。
Dp[i][j] = Dp[i-1][j-1]j+Dp[i-1][j]j
再用组合数分配一下两个部分的颜色。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

int n, m, k;
LL Dp[410][55];
LL c[55][55];

void Init()
{
    memset(c, 0, sizeof(c));
    for (int i = 0;i < 55;i++)
    {
        c[i][0] = 1;
        for (int j = 1;j <= i;j++)
        {
            c[i][j] = (c[i-1][j-1] + c[i-1][j])%MOD;
        }
    }
    for (int i = 1;i < 410;i++)
    {
        Dp[i][1] = 1;
        for (int j = 2;j < 55;j++)
        {
            Dp[i][j] = (Dp[i-1][j-1]*j+Dp[i-1][j]*j)%MOD;
        }
    }
}

int main()
{
    // freopen("test.in", "r", stdin);
    Init();
    int t, cas = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cas);
        scanf("%d%d%d", &n, &m, &k);
        n++, m++;
        LL ans = 0;
        if (n == m && m == 1)
            ans = k;
        else
        {
            int sum1 = 0, sum2 = 0;
            sum1 = ((n+1)/2)*((m+1)/2)+(n/2)*(m/2);
            sum2 = n*m-sum1;
            for (int i = 1;i < k;i++)
            {
                for (int j = 1;i+j <= k;j++)
                    ans = (ans + c[k][i]*c[k-i][j]%MOD*Dp[sum1][i]%MOD*Dp[sum2][j]%MOD)%MOD;
            }
        }
        printf(" %lld\n", ans);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12019331.html

时间: 2024-10-08 23:52:35

LightOJ - 1246 - Colorful Board(DP)的相关文章

LightOJ - 1246 Colorful Board(DP+组合数)

http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间不能涂相同的颜色,每个格子都必须有颜色,问可行的方案数. 分析 经一波分析,根据曼哈顿距离为奇数这一信息,可以将棋盘分为两部分,也就是相邻格子不能有相同颜色.一种颜色只能在一个部分中出现.现在考虑对一个部分的格子操作, dp[i][j]表示i个格子选择用了j种颜色的方案数,于是可以得到这样的递推式:

1246 - Colorful Board

   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are given a rectangular board. You are asked to draw M horizontal lines and N vertical lines in that board, so that the whole board will be divided into (M+1) x (N+1) c

Lightoj 1044 - Palindrome Partitioning (DP)

题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不是回文串处理出来,然后用dp[i] 表示 从起点到串i的位置的最少分割次数,然后结合处理出来的回文串转移一下即可! 还是好蠢哦!自己竟然感觉是一个区间DP,但是n又那么大,完全不是区间DP的作风啊! 1 #include <cmath> 2 #include <cstdio> 3 #i

lightoj 1381 - Scientific Experiment dp

1381 - Scientific Experiment Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lightoj.com/volume_showproblem.php?problem=1381 Description John wants to be a scientist. A first step of becoming a scientist is to perform experiment. John has de

LightOJ 1030 【概率DP求期望】

借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步,然后把那个格子的金子拿走: 如果扔出的骰子+所在位置>n,就重新扔,直到在n: 问取走这些值的期望值是多少 解析: [1] [2] [3][4] [5] [6] [7] [8] [9] //格子和值都是一样,所以下述的话,值就是格子,格子就是值... 比如这样的9个格子,我们总底往上来 对于第9个格

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

Lightoj 1071 - Baker Vai (双线程DP)

题目连接: http://lightoj.com/volume_showproblem.php?problem=1071 题目大意: 一个n*m的格子,Baker Vai要从(1,1)到(n,m)再回到(1,1),每到一个格子可以收集格子上的数字(每个格子只能走一次,(1,1)这个格子除外),问最终搜集的数字之和最大为多少? 解题思路: 可以把题目转化为求两个对象同时从(1,1)出发到(n,m)途中不能相遇,状态转移的时候可以用dp[step][x][y],step代表当前步数,x,y分别代表两

hdu 5693 &amp;&amp; LightOj 1422 区间DP

hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3组合成. 区间DP,用dp[i][j]表示在i到j之间可以删除的最大数,枚举区间长度,再考虑区间两端是否满足等差数列(这是考虑两个数的),再i到j之间枚举k,分别判断左端点右端点和k是否构成等差数列(还是考虑两个数的),判断左端点,k,右端点是否构成等差数列(这是老驴三个数的) 1 #include

LightOJ 1422 Halloween Costumes 区间dp

题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新dp[i][j]时有两种情况 考虑第i天穿的衣服 1:第i天穿的衣服在之后不再穿了 那么 dp[i][j]=dp[i+1][j]+1; 2:第i天穿的衣服与i+1到j的某一天共用,那么dp[i][j]=min(dp[i][j],dp[i+1][k-1],dp[k][j]),前提是第i天和第k天需要的礼