C. Eleventh Birthday

Codeforces
IDCF856C
Time2000ms
Memory512MB
Difficulty
combinatoricsdpmath
English · Original
Chinese · Translation
Formal · Original
It is Borya's eleventh birthday, and he has got a great present: _n_ cards with numbers. The _i_\-th card has the number _a__i_ written on it. Borya wants to put his cards in a row to get one greater number. For example, if Borya has cards with numbers 1, 31, and 12, and he puts them in a row in this order, he would get a number 13112. He is only 11, but he already knows that there are _n_! ways to put his cards in a row. But today is a special day, so he is only interested in such ways that the resulting big number is divisible by eleven. So, the way from the previous paragraph is good, because 13112 = 1192 × 11, but if he puts the cards in the following order: 31, 1, 12, he would get a number 31112, it is not divisible by 11, so this way is not good for Borya. Help Borya to find out how many good ways to put the cards are there. Borya considers all cards different, even if some of them contain the same number. For example, if Borya has two cards with 1 on it, there are two good ways. Help Borya, find the number of good ways to put the cards. This number can be large, so output it modulo 998244353. ## Input Input data contains multiple test cases. The first line of the input data contains an integer _t_ — the number of test cases (1 ≤ _t_ ≤ 100). The descriptions of test cases follow. Each test is described by two lines. The first line contains an integer _n_ (1 ≤ _n_ ≤ 2000) — the number of cards in Borya's present. The second line contains _n_ integers _a__i_ (1 ≤ _a__i_ ≤ 109) — numbers written on the cards. It is guaranteed that the total number of cards in all tests of one input data doesn't exceed 2000. ## Output For each test case output one line: the number of ways to put the cards to the table so that the resulting big number was divisible by 11, print the number modulo 998244353. [samples]
Borya 度过了他的十一岁生日,收到了一份很棒的礼物:#cf_span[n] 张写有数字的卡片。第 #cf_span[i] 张卡片上写着数字 #cf_span[ai]。Borya 想把这些卡片排成一行,组成一个尽可能大的数。例如,如果 Borya 有数字为 #cf_span[1]、#cf_span[31] 和 #cf_span[12] 的卡片,他按此顺序排列,会得到数字 #cf_span[13112]。 他才 11 岁,但他已经知道有 #cf_span[n!] 种方式将卡片排成一行。但今天是个特殊的日子,他只关心那些最终组成的大数能被 11 整除的排列方式。因此,上一段中的排列是可行的,因为 #cf_span[13112 = 1192 × 11];但如果他按 #cf_span[31]、#cf_span[1]、#cf_span[12] 的顺序排列,则得到数字 #cf_span[31112],它不能被 #cf_span[11] 整除,因此这种方式对 Borya 不合适。请帮助 Borya 计算有多少种合适的排列方式。 Borya 认为所有卡片都是不同的,即使其中一些卡片上的数字相同。例如,如果 Borya 有两张写有 1 的卡片,则存在两种合适的排列方式。 请帮助 Borya 计算合适的排列方式的数量。这个数可能很大,请对 #cf_span[998244353] 取模后输出。 输入数据包含多个测试用例。输入的第一行包含一个整数 #cf_span[t] —— 测试用例的数量(#cf_span[1 ≤ t ≤ 100])。接下来是各个测试用例的描述。 每个测试用例由两行组成。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 2000])—— Borya 礼物中卡片的数量。 第二行包含 #cf_span[n] 个整数 #cf_span[ai](#cf_span[1 ≤ ai ≤ 10^9])—— 卡片上写的数字。 保证所有测试用例中卡片总数不超过 #cf_span[2000]。 对于每个测试用例,输出一行:将卡片排成一行使得组成的大数能被 11 整除的方案数,结果对 #cf_span[998244353] 取模。 ## Input 输入数据包含多个测试用例。输入的第一行包含一个整数 #cf_span[t] —— 测试用例的数量(#cf_span[1 ≤ t ≤ 100])。接下来是各个测试用例的描述。每个测试用例由两行组成。第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 2000])—— Borya 礼物中卡片的数量。第二行包含 #cf_span[n] 个整数 #cf_span[ai](#cf_span[1 ≤ ai ≤ 10^9])—— 卡片上写的数字。保证所有测试用例中卡片总数不超过 #cf_span[2000]。 ## Output 对于每个测试用例,输出一行:将卡片排成一行使得组成的大数能被 11 整除的方案数,结果对 #cf_span[998244353] 取模。 [samples]
Let $ n $ be the number of cards, and let $ a_i \in \mathbb{Z}^+ $ be the number written on the $ i $-th card ($ 1 \leq i \leq n $). Each card is distinct (even if values repeat), so we consider all $ n! $ permutations of the cards. For a permutation $ \pi \in S_n $, let $ N_\pi $ denote the integer formed by concatenating the decimal representations of $ a_{\pi(1)}, a_{\pi(2)}, \dots, a_{\pi(n)} $ in that order. We are to count the number of permutations $ \pi $ such that: $$ N_\pi \equiv 0 \pmod{11} $$ --- ### Key Insight: Divisibility by 11 A number is divisible by 11 if and only if the alternating sum of its digits is divisible by 11. However, since we are concatenating entire numbers (not digits), we use the following property: Let $ x $ be a number with decimal representation $ d_k d_{k-1} \dots d_0 $. Then: $$ x \equiv \sum_{i=0}^k d_i \cdot (-1)^i \pmod{11} $$ Thus, for a concatenated number formed by numbers $ b_1, b_2, \dots, b_n $, we can compute its value modulo 11 as: $$ \sum_{j=1}^n \left( b_j \cdot (-1)^{s_j} \right) \pmod{11} $$ where $ s_j $ is the total number of digits in all numbers to the *right* of $ b_j $ (i.e., the position offset from the right end). Let $ L_j = \text{number of digits in } a_j $. Then, if $ a_j $ is placed at position $ i $ (from left) in the permutation, then the number of digits to its right is: $$ R = \sum_{k \text{ after } j} L_k $$ So the contribution of $ a_j $ to the total alternating sum modulo 11 is: $$ a_j \cdot (-1)^R \pmod{11} $$ Note: $ (-1)^R = (-1)^{\sum_{k \text{ after } j} L_k} = \prod_{k \text{ after } j} (-1)^{L_k} $ So define for each card $ j $: - $ d_j = \text{number of digits of } a_j $ - $ c_j = a_j \mod 11 $ - $ \sigma_j = (-1)^{d_j} \in \{1, -1\} $ Then, if we fix an ordering $ \pi $, the total alternating sum modulo 11 is: $$ \sum_{i=1}^n c_{\pi(i)} \cdot \prod_{k=i+1}^n \sigma_{\pi(k)} \pmod{11} $$ Let $ P_i = \prod_{k=i+1}^n \sigma_{\pi(k)} $. Then $ P_i = P_{i+1} \cdot \sigma_{\pi(i+1)}^{-1} $, and $ P_n = 1 $. Alternatively, define a **state** as the cumulative product of $ \sigma $'s from the end. Let’s define: - Let $ \tau_j = (-1)^{d_j} $ (i.e., $ \sigma_j $) - Let $ S = \prod_{j=1}^n \tau_j $ — the total sign product over all cards. Then, if we place card $ j $ at position $ i $, and let $ T_i $ be the product of $ \tau $'s of all cards to the *right* of position $ i $, then: $$ \text{Contribution of card } j \text{ at position } i = c_j \cdot T_i $$ But $ T_i $ depends on which cards are to the right — so we need to track the **product of $ \tau $'s for the suffix**. This suggests a **dynamic programming** approach: --- ### Formal DP Formulation Let: - $ n $: number of cards - For each card $ i $, define: - $ d_i = \lfloor \log_{10}(a_i) \rfloor + 1 $ (number of digits) - $ \tau_i = (-1)^{d_i} \in \{1, -1\} $ - $ c_i = a_i \mod 11 $ We want to count the number of permutations $ \pi $ such that: $$ \sum_{i=1}^n c_{\pi(i)} \cdot \left( \prod_{j=i+1}^n \tau_{\pi(j)} \right) \equiv 0 \pmod{11} $$ Let’s define DP state: - $ \text{dp}[i][s][r] $ = number of ways to assign the first $ i $ cards (in some order) such that: - $ s $ is the **product of $ \tau $'s** of the remaining (unplaced) cards (i.e., the suffix product multiplier), - $ r $ is the **current alternating sum modulo 11** contributed by the placed cards. Wait — better idea: process cards one by one and build the permutation from **right to left**. ### Better: Process from Right to Left Let’s build the number from right to left (i.e., place cards in reverse order). Then the rightmost card has exponent $ (-1)^0 = 1 $, the one to its left has $ (-1)^{d_{\text{right}}} $, etc. Define: - Let $ \text{dp}[mask][prod][sum] $ be the number of ways to assign cards corresponding to bitmask $ mask $, such that: - $ prod $ is the product of $ \tau $'s of all cards **already placed** (i.e., to the right of the current position) — this will be the multiplier for the next card to the left. - $ sum $ is the current total alternating sum modulo 11. But $ n \leq 2000 $, so bitmask is impossible. Alternative insight: since $ \tau_i \in \{1, -1\} $, the product of any subset is also $ \pm 1 $. So $ prod \in \{1, -1\} $, and $ sum \in \mathbb{Z}_{11} $. So we can use DP over: - $ \text{dp}[i][p][r] $: number of ways to assign $ i $ cards such that the product of $ \tau $'s of the assigned cards is $ p \in \{1, -1\} $, and the total alternating sum mod 11 is $ r $. Wait — no. The multiplier for a card placed now depends on the **product of $ \tau $'s of cards already placed to its right**. So if we build the permutation from **right to left**, then: - Start with no cards placed: product = 1, sum = 0 - When we place a card $ j $ to the left of the current sequence: - Its contribution: $ c_j \cdot (\text{current product}) $ - Then update product: $ \text{new product} = \text{current product} \cdot \tau_j $ - Update sum: $ \text{new sum} = (\text{current sum} + c_j \cdot \text{current product}) \mod 11 $ So state: - $ \text{dp}[i][p][r] $: number of ways to have placed $ i $ cards, with the product of $ \tau $'s of the placed cards being $ p \in \{1, -1\} $, and total alternating sum mod 11 being $ r \in \{0,1,\dots,10\} $ We iterate over all cards, and for each unplaced card, we try placing it to the left. But we have $ n \leq 2000 $, and $ p \in \{1, -1\} $, $ r \in \mathbb{Z}_{11} $, so state space is $ O(n \cdot 2 \cdot 11) = O(44000) $, which is acceptable. But we need to account for **identical values**: since cards are distinct, we must consider each card individually, even if values are same. So we need to iterate over all cards and use DP over subsets? No — we can use DP that doesn’t care which card, but counts multiplicities. Actually, we can group cards by $ (d_i, a_i \mod 11) $, i.e., by $ (\tau_i, c_i) $. Let’s define: - Group cards by type: each type is a pair $ (\tau, c) $, where $ \tau \in \{1, -1\} $, $ c \in \{0,1,\dots,10\} $ - Let $ f[\tau][c] $ be the count of cards of type $ (\tau, c) $ Then we can do DP over: - $ \text{dp}[p][r] $: number of ways to assign a multiset of cards (with counts reduced) such that: - $ p \in \{1, -1\} $: product of $ \tau $'s of all cards already placed (to the right) - $ r \in \mathbb{Z}_{11} $: current alternating sum mod 11 We iterate over all card types, and for each type, we decide how many cards of that type to place, and in what order? No — since the order of placing cards of the same type doesn’t matter in terms of state, but we need to account for permutations. Actually, since cards are distinct, we must account for **which specific card** is placed, but if two cards have same $ (\tau, c) $, they are interchangeable in terms of state transition. So we can do: Let $ \text{dp}[p][r] $ be the number of ways to assign a subset of cards (with multiplicity) such that the product of $ \tau $'s of placed cards is $ p $, and the sum is $ r $. We process cards one by one, and for each card, we can choose to place it next (to the left), updating state. But since total $ n \leq 2000 $, and we have up to 2000 cards, and state is $ 2 \times 11 = 22 $, we can do DP over cards: Initialize: $ \text{dp}[1][0] = 1 $ For each card $ i $ (from 1 to $ n $), we update: New state: for each existing state $ (p, r) $, we can place card $ i $ to the left of the current sequence. Then: - New product: $ p' = p \cdot \tau_i $ - New sum: $ r' = (r + c_i \cdot p) \mod 11 $ And we add $ \text{dp}[p][r] $ to $ \text{dp}[p'][r'] $ But this assumes we are building the permutation one card at a time, and each card is used exactly once. This is **correct** and the state space is $ O(n \cdot 2 \cdot 11) = O(44000) $, which is acceptable for $ n \leq 2000 $. But note: the order of processing cards matters? No — we are iterating over all cards, and each card is used once. Since the transition is linear and we process each card once, this is equivalent to counting all permutations. Wait — no: in this DP, we are **not** accounting for the fact that we are building permutations — we are just accumulating the total number of ways to assign cards in some order. But since we are considering each card exactly once, and we are updating the state by placing the next card to the left, this DP naturally counts **all permutations**. Yes: the recurrence is: $$ \text{dp}_{\text{new}}[p \cdot \tau_i][(r + c_i \cdot p) \mod 11] \mathrel{+}= \text{dp}_{\text{old}}[p][r] $$ for each card $ i $, and we start with one state and add cards one by one. This is **exactly** the standard DP for counting permutations with state, and it gives the total number of permutations satisfying the condition. But we must process cards **one by one**, and for each card, we update the entire state table. Algorithm: - Initialize: $ \text{dp}[1][0] = 1 $ - For each card $ i = 1 $ to $ n $: - Create new_dp = copy of current dp - For each state $ (p, r) $ in current dp: - Let $ \tau = \tau_i $, $ c = c_i $ - New product: $ p' = p \cdot \tau $ - New sum: $ r' = (r + c \cdot p) \mod 11 $ - Add $ \text{dp}[p][r] $ to $ \text{new\_dp}[p'][r'] $ - Set $ \text{dp} = \text{new\_dp} $ Wait — this is **incorrect**. Why? Because we are not choosing *which* card to place next — we are forcing an order of processing. But we want to count **all permutations**, so we need to consider that each card is placed in some position. Actually, the above DP **does** count all permutations, because we are iterating over all cards and for each card, we are inserting it at the **left end** of the current sequence. Since every permutation can be built by successively inserting cards at the left, and the state captures the cumulative effect, this is correct. But note: the transition assumes we are placing the card to the left of the current sequence. Since the multiplier for the new card is the product of $ \tau $'s of the cards already placed (to its right), this is exactly what we need. And since we process all cards, and each card is used once, the total number of sequences built is $ n! $, and the DP counts each permutation exactly once. Therefore, the final answer is $ \text{dp}[p][0] $ for any $ p \in \{1, -1\} $, because we only care that the total sum mod 11 is 0. But note: we don't care about the final product $ p $, only the sum. So: $$ \boxed{\text{Answer} = \left( \text{dp}[1][0] + \text{dp}[-1][0] \right) \mod 998244353} $$ We must handle $ -1 \mod 11 $ as 10, but in our state, we can store $ p \in \{1, 10\} $ (since $ -1 \equiv 10 \mod 11 $), but actually for $ p $, we are multiplying integers, so we can store $ p \in \{1, -1\} $ as integers, and do arithmetic mod 11 only for the sum. In code, we can represent $ p $ as 1 or -1 (integers), and do: - $ p' = p \cdot \tau_i $ → result is $ \pm 1 $ - $ r' = (r + c_i \cdot p) \mod 11 $ We'll use a 2D DP array: $ \text{dp}[2][11] $, where index 0 for $ p=1 $, index 1 for $ p=-1 $ Initialize: $ \text{dp}[0][0] = 1 $ For each card $ i $: - Let $ \tau = (-1)^{d_i} $, $ c = a_i \mod 11 $ - Create new_dp[2][11] = {0} - For each $ p\_idx \in \{0,1\} $, $ r \in 0..10 $: - If $ \text{dp}[p\_idx][r] > 0 $: - $ p = 1 $ if $ p\_idx == 0 $, else $ -1 $ - $ new\_p = p \cdot \tau $ - $ new\_p\_idx = 0 $ if $ new\_p == 1 $, else 1 - $ new\_r = (r + c \cdot p) \mod 11 $ - $ \text{new\_dp}[new\_p\_idx][new\_r] \mathrel{+}= \text{dp}[p\_idx][r] $ - Set $ \text{dp} = \text{new\_dp} $ After all cards, answer = $ (\text{dp}[0][0] + \text{dp}[1][0]) \mod 998244353 $ This is efficient: $ O(n \cdot 2 \cdot 11) = O(22n) \leq 44000 $ per test case. Total cards across test cases ≤ 2000, so total operations ≤ 2000 * 22 = 44000 — very feasible. --- ### Final Mathematical Formulation Let $ n \in \mathbb{N} $, and for $ i = 1, \dots, n $, let: - $ d_i = \lfloor \log_{10}(a_i) \rfloor + 1 $ - $ \tau_i = (-1)^{d_i} \in \{1, -1\} $ - $ c_i = a_i \mod 11 \in \{0, 1, \dots, 10\} $ Let $ \text{dp}[p][r] $ be the number of ways to assign a subset of cards (processed so far) such that: - $ p \in \{1, -1\} $: the product of $ \tau $'s of the placed cards (i.e., the multiplier for the next card to the left) - $ r \in \mathbb{Z}_{11} $: the current alternating sum mod 11 Initialize: $ \text{dp}[1][0] = 1 $, all others 0. For each card $ i = 1 $ to $ n $: $$ \text{new\_dp}[p \cdot \tau_i][(r + c_i \cdot p) \mod 11] \mathrel{+}= \text{dp}[p][r] \quad \forall p \in \{1, -1\}, r \in \mathbb{Z}_{11} $$ Then, the answer is: $$ \left( \text{dp}[1][0] + \text{dp}[-1][0] \right) \mod 998244353 $$ This counts the number of permutations of the $ n $ cards such that the concatenated number is divisible by 11.
Samples
Input #1
4
2
1 1
3
1 31 12
3
12345 67 84
9
1 2 3 4 5 6 7 8 9
Output #1
2
2
2
31680
API Response (JSON)
{
  "problem": {
    "name": "C. Eleventh Birthday",
    "description": {
      "content": "It is Borya's eleventh birthday, and he has got a great present: _n_ cards with numbers. The _i_\\-th card has the number _a__i_ written on it. Borya wants to put his cards in a row to get one greater ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF856C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "It is Borya's eleventh birthday, and he has got a great present: _n_ cards with numbers. The _i_\\-th card has the number _a__i_ written on it. Borya wants to put his cards in a row to get one greater ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Borya 度过了他的十一岁生日,收到了一份很棒的礼物:#cf_span[n] 张写有数字的卡片。第 #cf_span[i] 张卡片上写着数字 #cf_span[ai]。Borya 想把这些卡片排成一行,组成一个尽可能大的数。例如,如果 Borya 有数字为 #cf_span[1]、#cf_span[31] 和 #cf_span[12] 的卡片,他按此顺序排列,会得到数字 #cf_span[131...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of cards, and let $ a_i \\in \\mathbb{Z}^+ $ be the number written on the $ i $-th card ($ 1 \\leq i \\leq n $).\n\nEach card is distinct (even if values repeat), so we consider all ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments