Solution

首先我们要有敏锐的直觉: 我们将每一列中不选哪种颜色看作是一个序列, 则我们发现这个序列要求相邻两位的颜色不同. 我们还发现, 一个这样的序列对应两种不同的合法的棋盘, 因此统计合法棋盘数的问题, 就转化为了统计合法序列数.

我们不妨设\(R > G > B\). 我们可以用R将这个序列切成\(R - 1\)或\(R\)或\(R + 1\)段, 这取决于序列的开头/结尾是否放R. 一旦确定了序列开头和结尾是否放R, 我们在后面就只考虑在序列中间放R, 而不考虑开头和结尾. 每一段由G和B交错组成. 我们将这些由G和B组成的段分为以下三类:

  • GBGBGB...GB 或 BGBGBG...BG, 即B的个数与G的个数相同. 我们记这样的段有\(x\)段.
  • GBGB...GBG, 即G的个数比B多\(1\). 我们记这样的段有\(y\)段.
  • BGBG...BGB, 即B的个数比G多1. 我们记这样的段有\(z\)段.

则我们发现\(y - z\)的值是固定的: \(y - z = G - B\).

我们假设\(R\)将该序列分为\(d\)段(根据前文, \(或或d = R - 1或d = R + 1或d = R\)), 则\(x + y + z = d\). 我们再枚举\(x\), 则\(y\)与\(z\)的数量也被确定了. 我们把第二和第三种情况的序列补全为G和B数量相等的情况, 则现在我们总共有\(\frac{G + B + y + z}{2}\)对GB或BG.

我们用插板法在这些BG或GB中插入\(d - 1\)个R即可.

#include <cstdio>
#include <algorithm> using namespace std;
const int M = (int)1e6, MOD = (int)1e9 + 7;
int pw[M + 1], fac[M + 1], facInv[M + 1];
inline int C(int a, int b)
{
if (a < b) return 0;
return (long long)fac[a] * facInv[a - b] % MOD * facInv[b] % MOD;
}
inline int getInverse(int a)
{
int res = 1;
for (int x = MOD - 2; x; a = (long long)a * a % MOD, x >>= 1) if (x & 1) res = (long long)res * a % MOD;
return res;
}
inline int work(int d, int x, int y)
{
int ans = 0;
for (int a = 0; a <= d - (x - y); ++ a) if (((d - a) - (x - y)) % 2 == 0)
{
int c = ((d - a) - (x - y)) / 2, b = x - y + c;
ans = (ans + (long long)C((x + y + b + c) / 2 - 1, d - 1) * C(d, a) % MOD * C(b + c, b) % MOD * pw[a] % MOD) % MOD;
}
return ans;
}
int main()
{ #ifndef ONLINE_JUDGE freopen("board.in", "r", stdin);
freopen("board.out", "w", stdout); #endif pw[0] = 1; for (int i = 1; i <= M; ++ i) pw[i] = pw[i - 1] * 2 % MOD;
fac[0] = facInv[0] = 1; for (int i = 1; i <= M; ++ i) fac[i] = (long long)fac[i - 1] * i % MOD, facInv[i] = getInverse(fac[i]);
int T; scanf("%d", &T);
for (int cs = 0; cs < T; ++ cs)
{
int m, R, G, B; scanf("%d%d%d%d", &m, &R, &G, &B);
R = m - R; G = m - G; B = m - B;
if (G < B) swap(G, B);
if (R < G) swap(R, G);
if (G < B) swap(G, B);
int ans = (long long)2 * ((long long)work(R - 1, G, B) + 2 * work(R, G, B) + work(R + 1, G, B)) % MOD;
printf("%d\n", ans);
}
}

NOI模拟题4 Problem C: 填格子(board)的更多相关文章

  1. NOI模拟题1 Problem A: sub

    题面 Sample Input 5 7 2 -1 -3 1 1 1 2 1 3 3 4 3 5 2 1 3 0 2 1 2 1 2 1 1 -3 2 Sample Output 2 4 5 2 HIN ...

  2. NOI模拟题6 Problem C: Circle

    Solution 首先这个矩阵, 很明显的就是Vandermonde矩阵. 我们有公式: \[ |F_n| = \prod_{1 \le j < i \le n} (a_i - a_j) \] ...

  3. NOI模拟题5 Problem A: 开场题

    Solution 注意到\(\gcd\)具有结合律: \[ \gcd(a, b, c) = \gcd(a, \gcd(b, c)) \] 因此我们从后往前, 对于每个位置\(L\), 找到每一段不同的 ...

  4. NOI模拟题4 Problem B: 小狐狸(fox)

    Solution 考虑分开统计朝向每一个方向的所有狐狸对答案的贡献. 比如说以向右为例, 我们用箭标表示每一只狐狸的方向, 用\('\)表示当前一步移动之前的每一只狐狸的位置. \[ \begin{a ...

  5. NOI模拟题4 Problem A: 生成树(mst)

    Solution 我们考虑答案的表达式: \[ ans = \sqrt{\frac{\sum_{i = 1}^{n - 1} (w_i - \overline{w})^2}{n - 1}} \] 其中 ...

  6. 花海漫步 NOI模拟题

    题目好像难以看懂? 题目大意 给出一个字符串\(S\),统计满足以下条件的\((i,j,p,q)\)的数量. \(i \leq j, p \leq q\) \(S[i..j],S[p..q]\)是回文 ...

  7. 神奇的矩阵 NOI模拟题

    神奇的矩阵 题目大意 有一个矩阵\(A\),第一行是给出的,接下来第\(x\)行,第\(y\)个元素的值为数字\(A_{x-1,y}\)在\(\{A_{x-1,1},A_{x-1,2},A_{x-1, ...

  8. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  9. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem I. Interest Targeting 模拟题

    Problem I. Interest Targeting 题目连接: http://codeforces.com/gym/100714 Description A unique display ad ...

随机推荐

  1. 谈一谈Tomcat中webSocket,Jetty WebSocket 和Spring WebSocket以及github中Java-WebSocket.jar分别对Socket协议的角色定位以及效果的不同点;

    开局先上一张图:(http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html)   当前截图来自于apache的tomcat官网(问:为 ...

  2. hdu3374 String Problem 最小最大表示法 最小循环节出现次数

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...

  3. mac攻略(八) -- 神器zsh和iterm2的配置

      1. 安装oh my zsh 安装命令: curl -L http://install.ohmyz.sh | sh 修改shell的方式: chsh -s /bin/zsh   2.安装cask( ...

  4. leetcode 【 Container With Most Water 】python 实现

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. IOS开发学习笔记023-UIToolBar的使用

    这里使用代码实现 大概过程: 1.创建工具条 2.创建插入条 3.添加头像.标签.删除按钮 4.点击头像获取标签信息 做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添 ...

  6. c#中dynamic ExpandoObject的用法

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. Linux编程之变量

    Bash变量与变量分类 变量命名规则 变量名必须以字母或下划线打头,名字中间只能由字母.数字和下划线组成 变量名的长度不得超过255个字符 变量名在有效的范围内必须是唯一的 在Bash中,变量的默认类 ...

  8. imx6移植librtmp

    一.openssl交叉编译 1.下载 https://www.openssl.org/source/ 版本不要太高,刚开始版本高了,有些函数取消了,链接不上 使用1.0.1f即可 2.编译成共享库 . ...

  9. BestCoder 2nd Anniversary/HDU 5719 姿势

    Arrange Accepts: 221 Submissions: 1401 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/2 ...

  10. wsl折腾记

    参考1 wsl在哪 C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndg ...