Atcoder 279G-At most 2 colors: Generating functions [Newbie Oriented].

Правка en15, от CristianoPenaldo, 2022-11-28 18:01:02

Part1: Introduction

Atcoder Beginner Contest 279 is good for learning generating functions. The English version tutorial of 279Ex has already been posted on the MathStackExchange by a kind person. Unfortunately, the English tutorial on 279G is not available. Now I would like to make a second-hand tutorial on 279G based on PCTprobability's idea. I spend a huge amount of time understanding this idea. For contestants at about my level, it is quite difficult to understand
the idea even if it is written in English, let alone it is written in Japanese only (My Japanese is N5 level). I will make the following contributions.

(1)Write the tutorial in English. The original tutorial is in Japanese only.

(2)Fill in the details.

(3)Offer an accepted implementation.

I have to state that, using generating function is definitely not the best way to solve this problem. It could be solved much simpler by using dynamic programming with monotone deque optimization. However, this problem is also a good chance to learn generating functions.

Part2: Problem Statement

The problem says: There is a grid with $$$1×N$$$ squares, numbered $$$1,2,…,N$$$ from left to right.

Takahashi prepared paints of $$$C$$$ colors and painted each square in one of the C colors. Then, there were at most two colors in any consecutive K squares. Formally, for every integer $$$i$$$ such that $$$1≤i≤N−K+1$$$, there were at most two colors in squares $$$i,i+1,…,i+K−1$$$.

In how many ways could Takahashi paint the squares? Since this number can be enormous, find it modulo $$$998244353$$$.

$$$\cdot \text{All inputs are integers.}$$$

$$$\cdot 2 \leq K \leq N \leq 10^6$$$.

$$$\cdot 1 \leq C \leq 10^9$$$.

Test Case $$$1$$$: $$$N=K=C=3$$$. In this input, we have a $$$1×3$$$ grid. Among the $$$27$$$ ways to paint the squares, there are $$$6$$$ ways to paint all squares in different colors, and the remaining $$$21$$$ ways are such that there are at most two colors in any consecutive three squares.

Test Case $$$2$$$: $$$N=10, K=5, C=2$$$: Print $$$1024$$$.

Test Case $$$3$$$: $$$N=998, K=244, C=353$$$: Print $$$952364159$$$.

Part3: Idea

(1) What are the generating functions (Gf) good at? Gf is good at solving partitions, for example, the Pentagonal number theorem. So, the first step is to compress the colors by Run-Length Encoding (RLE). For example, if the colors are $$$(1,1,1,2,2,3,2,2)$$$, then they are uniquely compressed to $$$((1, 3), (2, 2), (3, 1), (2, 2))$$$. With RLE, $$$[1, n]$$$ is partitioned into $$$l$$$ segments with different colors. Let me denote these segments as $$$S_1, S_2, ..., S_l$$$.

(2)Consider $$$2 \leq i \leq l-1$$$. If $$$|S_i| \leq K-2$$$, then $$$S_{i+1}$$$ only has one choice: Paint it with the same color as $$$S_{i-1}$$$. Otherwise, the last element of $$$S_{i-1}$$$, the whole segment $$$S_{i}$$$ and the first element of $$$S_{i+1}$$$ will form an interval with size $$$\leq K$$$ and three colors, violating the rule.

Теги combinatorics, generating function

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en48 Английский CristianoPenaldo 2022-11-29 06:22:07 10 Tiny change: 'he sum of segmen' -> 'he sum of length of segmen'
en47 Английский CristianoPenaldo 2022-11-28 20:37:43 40 Tiny change: 'unction). The Eng' -> 'unction). GFs could solve two of them (G and Ex). The Eng'
en46 Английский CristianoPenaldo 2022-11-28 20:26:58 2 Tiny change: '36865779\nCore cod' -> '36865779\n\nCore cod'
en45 Английский CristianoPenaldo 2022-11-28 20:23:46 2 Tiny change: 'e(n-1); //inverse!\n ' -> 'e(n-1); //Inverse!\n '
en44 Английский CristianoPenaldo 2022-11-28 20:23:20 137
en43 Английский CristianoPenaldo 2022-11-28 20:16:33 43
en42 Английский CristianoPenaldo 2022-11-28 19:46:39 6 Tiny change: 'orem).\n\nAnd the most im' -> 'orem).\n\nThe most im'
en41 Английский CristianoPenaldo 2022-11-28 19:43:34 5 Tiny change: '-2}]\frac{1}{(C-2)x^K' -> '-2}]\frac{C(C-1)}{(C-2)x^K'
en40 Английский CristianoPenaldo 2022-11-28 19:29:15 250 (published)
en39 Английский CristianoPenaldo 2022-11-28 19:25:12 927
en38 Английский CristianoPenaldo 2022-11-28 19:16:47 14
en37 Английский CristianoPenaldo 2022-11-28 19:15:16 14
en36 Английский CristianoPenaldo 2022-11-28 19:14:35 199
en35 Английский CristianoPenaldo 2022-11-28 19:10:22 82
en34 Английский CristianoPenaldo 2022-11-28 19:08:59 84
en33 Английский CristianoPenaldo 2022-11-28 19:08:14 9 Tiny change: 'ag{5}$\n\n$\sum\' -> 'ag{5}$\n\nAnd, \n\n$\sum\'
en32 Английский CristianoPenaldo 2022-11-28 19:06:51 42
en31 Английский CristianoPenaldo 2022-11-28 19:05:30 128
en30 Английский CristianoPenaldo 2022-11-28 19:03:06 296
en29 Английский CristianoPenaldo 2022-11-28 18:59:14 647
en28 Английский CristianoPenaldo 2022-11-28 18:52:33 514
en27 Английский CristianoPenaldo 2022-11-28 18:45:35 139
en26 Английский CristianoPenaldo 2022-11-28 18:42:58 200
en25 Английский CristianoPenaldo 2022-11-28 18:40:36 77
en24 Английский CristianoPenaldo 2022-11-28 18:38:58 286
en23 Английский CristianoPenaldo 2022-11-28 18:36:38 741
en22 Английский CristianoPenaldo 2022-11-28 18:26:28 261
en21 Английский CristianoPenaldo 2022-11-28 18:21:14 10 Tiny change: '1}{1-2x}$ as $\sum\lim' -> '1}{1-2x}$ to $\sum\lim'
en20 Английский CristianoPenaldo 2022-11-28 18:19:57 358
en19 Английский CristianoPenaldo 2022-11-28 18:16:43 680
en18 Английский CristianoPenaldo 2022-11-28 18:04:09 13 Tiny change: 'I believe most newbies can under' -> 'I believe you can under'
en17 Английский CristianoPenaldo 2022-11-28 18:03:41 5 Tiny change: 'I believe than most newb' -> 'I believe most newb'
en16 Английский CristianoPenaldo 2022-11-28 18:03:16 221
en15 Английский CristianoPenaldo 2022-11-28 18:01:02 53
en14 Английский CristianoPenaldo 2022-11-28 17:36:43 313
en13 Английский CristianoPenaldo 2022-11-28 17:28:37 150
en12 Английский CristianoPenaldo 2022-11-28 17:20:53 383
en11 Английский CristianoPenaldo 2022-11-28 17:17:25 608
en10 Английский CristianoPenaldo 2022-11-28 17:11:29 7
en9 Английский CristianoPenaldo 2022-11-28 17:11:05 105
en8 Английский CristianoPenaldo 2022-11-28 17:09:36 6
en7 Английский CristianoPenaldo 2022-11-28 17:09:05 167
en6 Английский CristianoPenaldo 2022-11-28 17:06:21 274
en5 Английский CristianoPenaldo 2022-11-28 17:05:04 524
en4 Английский CristianoPenaldo 2022-11-28 16:33:51 284
en3 Английский CristianoPenaldo 2022-11-28 16:31:38 487
en2 Английский CristianoPenaldo 2022-11-28 16:26:30 221
en1 Английский CristianoPenaldo 2022-11-28 16:23:40 360 Initial revision (saved to drafts)