E. Height All the Same

这篇博客探讨了一种n*m矩阵高度统一的问题,分析了如何通过两种操作使得所有格点高度相同。关键在于奇偶性分析,发现当奇数或偶数格点数量为偶数时可以通过操作实现。利用二项式定理,计算了不同情况下的答案,包括当矩阵大小为奇数和偶数时的不同策略,并提供了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


E. Height All the Same


标签

  • 组合数学
  • 二项式定理

简明题意

  • 给定n*m的矩阵,每一个a[i][j]代表(i,j)的高度。你可以执行两种操作:
  • 1.给任意一个a[i][j]加上2.
  • 2.给两个相邻的格子都加1.
  • 现在给出n,m,l,r,问你n*m的矩阵,每个格点的初始值是[l,r]中的任意一个,问有多少种初始值,可以使得经过上面的2种操作,所有格点的值相同。

思路

  • 假设每个格点的高度已知,那么如何判断是否能通过上面的操作使得全部相等呢?实际上可以从奇偶性的角度考虑。
  • 如果所有格点都是奇数或者都是偶数,那么很显然通过操作1即可。接下来考虑既有奇数格点又有偶数格点的情况。
  • 我们想想操作2的作用。显然是将奇偶性反转。假如有4个格点是奇数,那么可以通过操作2将这4个格点全部变成偶数。概括一下,也就是同奇偶性的数有偶数个,那么可以先通过操作2使得所有数的奇偶性相同,再通过操作1使得所有数相等。
  • 综上,所有数奇偶性相同可行(直接操作1),奇数有偶数种可行(先操作2再操作1),偶数有偶数种可行。唯一不可行的是:奇数和偶数都是奇数种。
  • 记下来思考如何计算答案。
  • 1.nm是奇数。那么奇数和偶数总有一种的个数是偶数。所以怎么填都可行。答案就是:每个点可以填L-R中的任意一个,也就是每个格点可以填(R-L)种数,一共有nm个格点,答案就是 ( R − L ) n ∗ m (R-L)^{n*m} (RL)nm
  • 2.n*m是偶数。
  • 那么我们先从n*m个格子中选出偶数个格子, C n ∗ m i C_{n*m}^{i} Cnmi。再把这些格子填成偶数, x i x^{i} xi,剩下的格子填奇数: y n ∗ m − i y^{n*m-i} ynmi,所以最后答案就是:
    ∑ i = 0 且 i 为 偶 数 n ∗ m C n ∗ m i x i y n ∗ m − i \sum_{i=0且i为偶数}^{n*m}C_{n*m}^{i}x^{i}y^{n*m-i}
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ........ ........ .CCC.... EEEEEE.. ........ ........ ..BBBB.. .C.C.... E....E.. DDDDDD.. ........ ..B..B.. .C.C.... E....E.. D....D.. ........ ..B..B.. .CCC.... E....E.. D....D.. ....AAAA ..B..B.. ........ E....E.. D....D.. ....A..A ..BBBB.. ........ E....E.. DDDDDD.. ....A..A ........ ........ E....E.. ........ ....AAAA ........ ........ EEEEEE.. ........ ........ ........ ........ 1 2 3 4 5 Now place them on top of one another starting with 1 at the bottom and ending up with 5 on top. If any part of a frame covers another it hides that part of the frame below. Viewing the stack of 5 frames we see the following. .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE.. In what order are the frames stacked from bottom to top? The answer is EDABC. Your problem is to determine the order in which the frames are stacked from bottom to top given a picture of the stacked frames. Here are the rules: 1. The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters. 2. It is possible to see at least one part of each of the four sides of a frame. A corner shows two sides. 3. The frames will be lettered with capital letters, and no two frames will be assigned the same letter. Input Each input block contains the height, h (h<=30) on the first line and the width w (w<=30) on the second. A picture of the stacked frames is then given as h strings with w characters each. Your input may contain multiple blocks of the format described above, without any blank lines in between. All blocks in the input must be processed sequentially. Output Write the solution to the standard output. Give the letters of the frames in the order they were stacked from bottom to top. If there are multiple possibilities for an ordering, list all such possibilities in alphabetical order, each one on a separate line. There will always be at least one legal ordering for each input block. List the output for all blocks in the input sequentially, without any blank lines (not even between blocks). Sample Input 9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE.. Sample Output EDABC
最新发布
06-06
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值