首页 > 其他分享 >G. One-Dimensional Puzzle

G. One-Dimensional Puzzle

时间:2024-02-14 20:34:19浏览次数:21  
标签:elements int Puzzle puzzle Dimensional 放置 each ldots

G. One-Dimensional Puzzle

You have a one-dimensional puzzle, all the elements of which need to be put in one row, connecting with each other. All the puzzle elements are completely white and distinguishable from each other only if they have different shapes.

Each element has straight borders at the top and bottom, and on the left and right it has connections, each of which can be a protrusion or a recess. You cannot rotate the elements.

You can see that there are exactly $4$ types of elements. Two elements can be connected if the right connection of the left element is opposite to the left connection of the right element.

All possible types of elements.

The puzzle contains $c_1, c_2, c_3, c_4$ elements of each type. The puzzle is considered complete if you have managed to combine all elements into one long chain. You want to know how many ways this can be done.

Input

The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of input test cases. The descriptions of the test cases follow.

The description of each test case contains $4$ integers $c_i$ ($0 \le c_i \le 10^6$) — the number of elements of each type, respectively.

It is guaranteed that the sum of $c_i$ for all test cases does not exceed $4 \cdot 10^6$.

Output

For each test case, print one integer — the number of possible ways to solve the puzzle.

Two methods are considered different if there is $i$, such that the types of elements at the $i$ position in these methods differ.

Since the answer can be very large, output it modulo $998244353$.

If it is impossible to solve the puzzle, print $0$.

Example

input

11
1 1 1 1
1 2 5 10
4 6 100 200
900000 900000 900000 900000
0 0 0 0
0 0 566 239
1 0 0 0
100 0 100 0
0 0 0 4
5 5 0 2
5 4 0 5

output

4
66
0
794100779
1
0
1
0
1
36
126

 

解题思路

  模拟一下可以发现任意一个合法方案中,只看 $1$ 和 $2$ 拼图的相当顺序的话,必然是交替出现的,即 $1 \, 2 \, 1 \, 2 \, 1 \ldots$ 或 $2 \, 1 \, 2 \, 1 \, 2 \ldots$。因为将 $3$ 或 $4$ 拼接到 $1$ 和 $2$ 左右两侧时,并不会改变图案左右两侧的凹凸性。所以如果 $|c_1 - c_2| \geq 2$ 则无解。

  同时可以发现 $3$ 只会出现在 $1$ 的右侧或 $2$ 的左侧,$4$ 只会出现在 $1$ 的左侧或 $2$ 的右侧,所以方案大致都是这样的形式:$$\ldots \overbrace{ \ldots 4 \ldots}^{\text{若干个 } 4} \, {\color{Red} 1} \, \overbrace{\ldots 3 \ldots}^{\text{若干个 } 3} \, {\color{Red} 2} \, \overbrace{\ldots 4 \ldots}^{\text{若干个 } 4} \, {\color{Red} 1} \, \overbrace{\ldots 3 \ldots}^{\text{若干个 } 3} \ldots$$

  另外可以发现 $3$ 和 $4$ 的放置是相互独立的,所以可以分别计算放置 $3$ 和 $4$ 的方案数,再通过乘法原理得到总的方案数。

  当 $c_1 \ne c_2$,只能是 $1 \, 2 \, \ldots 1$ 或 $2 \, 1 \, \ldots 2$ 中的一种,令 $m = \max\{c_1, c_2\}$,则可以放置 $3$ 和 $4$ 的位置均有 $m$ 个。先考虑放置 $3$ 的方案数,假设每个位置放置的数量为 $x_i \geq 0$,这相当于问 $x_1 + x_2 + \ldots + x_m = c_3$ 的解有多少组,根据隔板法知道有 $C_{c_3+m-1}^{m-1}$ 种。同理放置 $4$ 的方案数为 $C_{c_4+m-1}^{m-1}$。所以总的方案数为 $C_{c_3+m-1}^{m-1} \times C_{c_4+m-1}^{m-1}$。

  当 $c_1 = c_2$,那么可以是 $1 \, 2 \, \ldots 1 \, 2$ 或 $2 \, 1 \, \ldots 2 \, 1$ 这两种,对应到 $3$ 有 $m$ 个位置 $4$ 有 $m+1$ 个位置可放置,或 $4$ 有 $m$ 个位置 $3$ 有 $m+1$ 个位置可放置。把两种情况都考虑,则有 $C_{c_3+m-1}^{m-1} \times C_{c_4+m}^{m} + C_{c_3+m}^{m} \times C_{c_4+m-1}^{m-1}$ 种方案。另外需要特判 $c_1 = c_2 = 0$ 的情况,此时若 $c_3$ 和 $c_4$ 都不为 $0$ 则无解。

  AC 代码如下,时间复杂度为 $O(\max\{c_1, c_2\})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 998244353;

int qmi(int a, int k) {
    int ret = 1;
    while (k) {
        if (k & 1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        k >>= 1;
    }
    return ret;
}

int C(int a, int b) {
    int u = 1, d = 1;
    for (int i = 1, j = a; i <= b; i++, j--) {
        u = 1ll * u * j % mod;
        d = 1ll * d * i % mod;
    }
    return 1ll * u * qmi(d, mod - 2) % mod;
}

void solve() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (abs(a - b) >= 2) {
        printf("0\n");
        return;
    }
    int m = max(a, b);
    if (a != b) {
        printf("%d\n", 1ll * C(c + m - 1, m - 1) * C(d + m - 1, m - 1) % mod);
    }
    else {
        if (!m) printf("%d\n", !c || !d);
        else printf("%d\n", (1ll * C(c + m - 1, m - 1) * C(d + m, m) + 1ll * C(c + m, m) * C(d + m - 1, m - 1)) % mod);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 925 (Div. 3) A-G 比赛录屏+讲解(60分钟开始):https://www.bilibili.com/video/BV16p421d7xw/

标签:elements,int,Puzzle,puzzle,Dimensional,放置,each,ldots
From: https://www.cnblogs.com/onlyblues/p/18015557

相关文章

  • CodeForces 1931G One-Dimensional Puzzle
    洛谷传送门CF传送门什么[ABC336G]16Integers究极弱化版。把元素\(1\)看成\(01\),元素\(2\)看成\(10\),元素\(3\)看成\(11\),元素\(4\)看成\(00\)。则转化为统计长度为\(2\)的子串\(xy\)出现次数为\(c_{xy}\)的\(01\)串个数。把子串\(xy\)看成\(x\to......
  • Puzzle hunt 工具
    写在前面   做一下破解网站的汇总,方便调出来用,省的老在收藏夹韩信点兵()关于古典密码相关的网站指路古典密码篇→古典密码汇总-Tey729-博客园(cnblogs.com) 正文1.Qat-可定位字母找单词 Qat(quinapalus.com) 2.河马-搭配Qat食用,单词更多也更不靠谱 Th......
  • four-dimensional
    本文参考《从一到无穷大》(作者:George$\cdot$Gamow)第$4$章笔者知识浅薄,只是阅后拙见。可能有错漏之处,望高明的读者赐教。什么是四维空间众所周知,我们生活的三维空间中,确定物体的位置需要$3$个坐标:长,宽,高。四维空间,就是有$4$个维度。那么,第四维是什么?让我们想象一个情......
  • ABC336 F Rotation Puzzle 题解
    QuestionABC336FRotationPuzzle给出一个\(H\timesW\)的矩阵,里面填有数字,有一种操作选定一个\((x,y)\)交换\((i+x,j+y)\)和\((H-i+x,W-j+y)\)对于每一个\(1\lei\leH-1,1\lej\leW-1\)问,是否能经过\(20\)次以内的操作使得,最后的矩形变成\((i,j)=((i-1)\t......
  • 利用强化学习算法解释人类脑对高维状态的抽象表示:how humans can map high-dimensiona
    论文:《Usingdeepreinforcementlearningtorevealhowthebrainencodesabstractstate-spacerepresentationsinhigh-dimensionalenvironments》地址:https://www.cell.com/neuron/fulltext/S0896-6273(20)30899-0正文:https://www.cell.com/neuron/pdf/S0896-6273(20......
  • Animals and Puzzle 题解
    原题链接:CF713D题意:给定一个\(n\timesm\)的地图\(a\),\(a_{i}\)为\(0\)或\(1\)。有\(t\)次询问,每次询问给定一个矩形,求出这个矩形中最大的由\(1\)构成的正方形的边长是多少。首先考虑预处理出\(d_{i,j}\)表示以\((i,j)\)为左上角的最大正方形边长是多少。对于每......
  • 多因子降维法 multifactor dimensionality reduction MDR
    MDR的应用:在病例对照研究中,应用多因子降维法(MDR)分析基因-基因交互作用,较传统的统计学分析方法无法比拟的优势。Logistic回归的局限性理论上的不足:自变量对疾病的影响是独立的,但实际情况及推导结果不同。模型有不合理性:“乘法模型”与一般希望的“相加模型”相矛盾。最大似然......
  • CF1773J King's Puzzle 题解
    题意:思路:当$k\gen$时,一定无法构造。证明:$n$个点的无向图,每个点的度数$d∈[1,n-1]$,度数的种数一定不会超过$n-1$。当$k\len-1$时,构造方案如下:首先,选取前$k+1$个点,构造成一条链,此时链上各点的度数为$1$,$2$,$2$,$...$,$2......
  • The 2023 ICPC Asia Hefei Regional Contest Test I. Linguistics Puzzle
    Preface这题yysy真不难,但比赛的时候想出做法后没时间写了,只能遗憾地看着倒计时结束Solution直接上爆搜复杂度肯定会爆,考虑有哪些数是可以不用搜直接推出来的首先样例启发我们\(0,1\)这两个数很好确定,因为\(0\)对应的字母单独出现的次数肯定最多,而\(1\)作为两位的开头出现的次......
  • [ABC326D] ABC Puzzle 题解
    题目链接解法分析这个问题是一个经典的排列谜题,通过回溯算法来穷举所有可能的字符排列,然后验证是否满足行和列约束。这个解决方案可以用于解决类似的谜题,其中需要满足一定的排列条件。通过仔细考虑约束条件,可以加快解决问题的速度,减少不必要的计算。更详细的我写在代码里了。......