codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1844D.md (3160B)


      1 Source: https://codeforces.com/problemset/problem/1844/D
      2 
      3 D. Row Major
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The row-major order of an $r \times c$ grid of characters $A$ is the string obtained by concatenating all the rows, i.e. $$ A_{11}A_{12} \dots A_{1c}A_{21}A_{22} \dots A_{2c} \dots A_{r1}A_{r2} \dots A_{rc}. $$
     22 
     23 A grid of characters $A$ is bad if there are some two adjacent cells (cells sharing an edge) with the same character.
     24 
     25 You are given a positive integer $n$. Consider all strings $s$ consisting of only lowercase Latin letters such that they are not the row-major order of any bad grid. Find any string with the minimum number of distinct characters among all such strings of length $n$.
     26 
     27 It can be proven that at least one such string exists under the constraints of the problem.
     28 
     29 Input
     30 
     31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     32 
     33 The only line of each test case contains a single integer $n$ ($1 \le n \le 10^6$).
     34 
     35 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
     36 
     37 Output
     38 
     39 For each test case, output a string with the minimum number of distinct characters among all suitable strings of length $n$.
     40 
     41 If there are multiple solutions, print any of them.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 4
     49 
     50 4
     51 
     52 2
     53 
     54 1
     55 
     56 6
     57 ```
     58 
     59 Output
     60 
     61 ```
     62 that
     63 is
     64 a
     65 tomato
     66 ```
     67 
     68 Note
     69 
     70 In the first test case, there are $3$ ways $s$ can be the row-major order of a grid, and they are all not bad:
     71 
     72 |  |  |  |  |  |  |  |  |  |
     73 | --- | --- | --- | --- | --- | --- | --- | --- | --- |
     74 | t |  | t | h |  | t | h | a | t |
     75 | h |  | a | t |  |  |  |  |  |
     76 | a |  |  |  |  |  |  |  |  |
     77 | t |  |  |  |  |  |  |  |  |
     78 
     79 It can be proven that $3$ distinct characters is the minimum possible.
     80 
     81 In the second test case, there are $2$ ways $s$ can be the row-major order of a grid, and they are both not bad:
     82 
     83 |  |  |  |  |
     84 | --- | --- | --- | --- |
     85 | i |  | i | s |
     86 | s |  |  |  |
     87 
     88 It can be proven that $2$ distinct characters is the minimum possible.
     89 
     90 In the third test case, there is only $1$ way $s$ can be the row-major order of a grid, and it is not bad.
     91 
     92 In the fourth test case, there are $4$ ways $s$ can be the row-major order of a grid, and they are all not bad:
     93 
     94 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
     95 | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
     96 | t |  | t | o |  | t | o | m |  | t | o | m | a | t | o |
     97 | o |  | m | a |  | a | t | o |  |  |  |  |  |  |  |
     98 | m |  | t | o |  |  |  |  |  |  |  |  |  |  |  |
     99 | a |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
    100 | t |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
    101 | o |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
    102 
    103 It can be proven that $4$ distinct characters is the minimum possible. Note that, for example, the string "orange" is not an acceptable output because it has $6 \gt 4$ distinct characters, and the string "banana" is not an acceptable output because it is the row-major order of the following bad grid:
    104 
    105 |  |  |
    106 | --- | --- |
    107 | b | a |
    108 | n | a |
    109 | n | a |