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即可.

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int M = (int)1e6, MOD = (int)1e9 + 7;
  5. int pw[M + 1], fac[M + 1], facInv[M + 1];
  6. inline int C(int a, int b)
  7. {
  8. if (a < b) return 0;
  9. return (long long)fac[a] * facInv[a - b] % MOD * facInv[b] % MOD;
  10. }
  11. inline int getInverse(int a)
  12. {
  13. int res = 1;
  14. for (int x = MOD - 2; x; a = (long long)a * a % MOD, x >>= 1) if (x & 1) res = (long long)res * a % MOD;
  15. return res;
  16. }
  17. inline int work(int d, int x, int y)
  18. {
  19. int ans = 0;
  20. for (int a = 0; a <= d - (x - y); ++ a) if (((d - a) - (x - y)) % 2 == 0)
  21. {
  22. int c = ((d - a) - (x - y)) / 2, b = x - y + c;
  23. 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;
  24. }
  25. return ans;
  26. }
  27. int main()
  28. {
  29. #ifndef ONLINE_JUDGE
  30. freopen("board.in", "r", stdin);
  31. freopen("board.out", "w", stdout);
  32. #endif
  33. pw[0] = 1; for (int i = 1; i <= M; ++ i) pw[i] = pw[i - 1] * 2 % MOD;
  34. 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]);
  35. int T; scanf("%d", &T);
  36. for (int cs = 0; cs < T; ++ cs)
  37. {
  38. int m, R, G, B; scanf("%d%d%d%d", &m, &R, &G, &B);
  39. R = m - R; G = m - G; B = m - B;
  40. if (G < B) swap(G, B);
  41. if (R < G) swap(R, G);
  42. if (G < B) swap(G, B);
  43. int ans = (long long)2 * ((long long)work(R - 1, G, B) + 2 * work(R, G, B) + work(R + 1, G, B)) % MOD;
  44. printf("%d\n", ans);
  45. }
  46. }

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. rest_framework序列化

    1.序列化 1)拿到queryset 2)将queryset 给序列化类 serializer = IdcSerializer(idc)    #单个对象 serializer = IdcSerial ...

  2. 使用WMI Filter 实现组策略的筛选!

    今天接到一个客户的一个问题,提到需要分系统版本分发相应的MSI程序.比如简体版接受简体版的分发程序,繁体版接受繁体版的分发程序!这个建立组策略的不同版本分发本身不会太难,我们只需要建立两个不同组策略分 ...

  3. JAVA后端常用框架SSM,redis,dubbo等

    JAVA后端常用框架SSM,redis,dubbo等   一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 spri ...

  4. 通过APP,网页打开手机客户端QQ

    以下内容为转载,原帖子 http://m.blog.csdn.net/blog/qduningning/40587099 在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用.而在移 ...

  5. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  6. 成都大学CTF 网络攻防演练平台 WP

    web1 输入框那里鼠标右键,审查元素,删除maxlength web2 http://ctf.cdusec.org:8082/web2/?cdusec=tql web3 同上,用火狐hackbar或 ...

  7. python 笔试总结

    1.对比两种函数对应结果 def fn(x): if x>0: print(x) fn(x-1) ****结果****** 3 2 1 $$$$$$另外一种$$$$$$$$$ def fn(x) ...

  8. hadoop配置文件: hdfs-site.xml, mapred-site.xml

    dfs.name.dir Determines where on the local filesystem the DFS name node should store the name table( ...

  9. ActionContext源码

    /* * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License, ...

  10. s debug

    value stack contents   ognl 值栈 stack context           action上下文 action上下文是一个map对象,通过#key获得对象内容,在#re ...