Writing the statement: Markdown and formulas β€” MOJ docs

Writing the statement: Markdown and formulas

Translation note. This manual is a translation of the Portuguese original. The command-line tools (moj, moj-contest, moj-comp) print their messages in Portuguese, and the command examples below are identical to the original.

This guide tells you how to write the docs/enunciado.md file. It has examples that you can copy. The PACOTE document gives the package format (the location of each file, the title, the samples, the languages). This page is about the text: the formatting and, most importantly, the formulas.

The statement is Markdown (the pandoc dialect) with formulas in TeX between $ signs. The same text becomes two things:

enunciado.org and enunciado.tex are also valid. But .md is the canonical format, and it is the only format that translations accept. This page uses Markdown.

1. The skeleton

Joana has a sequence of $N$ integers. She wants to know how many pairs of positions $(i, j)$, with
$i < j$, have an even sum.

## Input

The first line contains an integer $N$ ($1 \le N \le 2 \cdot 10^5$). The second line contains
$N$ integers $a_1, a_2, \ldots, a_N$ ($|a_i| \le 10^9$).

## Output

Print a single integer: the number of pairs.

The validation enforces or warns about three rules:

  1. ## Input and ## Output are required. The Portuguese headings ## Entrada/## SaΓ­da are also valid (and so are the Spanish ## Entrada/## Salida). You can add other sections freely: ## Notes, ## Remarks, ## Subtasks…
  2. Do not put the title in the text. The title is a field of the problem. A % Title on the first line is legacy, and the system removes it.
  3. Do not put the samples in the text. They come from tests/input/sample*, and they appear automatically at the end. Put the explanation of each sample in docs/notes/sample1.md (see PACOTE).

2. Text

You write Result PDF
*italic* or _italic_ italic βœ“
**bold** bold βœ“
`abacaba` (code, strings, file names) abacaba βœ“
[text](https://exemplo.org) text βœ“ (the text)
## Section / ### Subsection section heading βœ“
R\$ 10,00 R$ 10,00 βœ“
\* \_ \# (the character, without formatting) * _ # βœ“

A blank line separates paragraphs. A line break inside a paragraph changes nothing (the text stays in the same paragraph). You can write as you like, for example one sentence per line.

Lists: use - or numbers, and put a blank line before the list:

The operations are:

- `ADD x`: inserts $x$ into the set;
- `DEL x`: removes $x$;
- `QRY`: prints the smallest element.

1. first step;
2. second step.

Table (the --- line is required; :---: centers the column):

| Subtask   | Points | Constraints       |
|:---------:|:------:|-------------------|
| 1         | 20     | $N \le 100$       |
| 2         | 80     | no constraints    |

Code block (an illustrative input, a piece of a program): put three backticks before and after it:

```
3
1 2 3
```

Quotation: start the line with > .

Centered text: the ::: center block (section 3, "Center") also applies to text.

Images: see section 3.

Typography

You write Result PDF
1--10 (en dash, for ranges) 1–10 βœ“
pause --- like this (em dash) pause β€” like this βœ“
"quotes" and 'single' (they become curly automatically) β€œquotes” and β€˜single’ βœ“
... … βœ“
10\ km (a space that does not break the line) 10 km βœ“
~~strikethrough~~ strikethrough βœ“
[underlined]{.underline} underlined βœ“
H~2~O, 2^10^ (subscript and superscript in the text; in a formula, use $…$) H2O, 210 βœ“
<!-- author's note --> (does not appear) βœ“
--- on its own line, with a blank line before it horizontal rule βœ“
line that ends with \ (forced line break) breaks the line ⚠ the line before the break comes out stretched (the PDF is justified); use separate paragraphs or a list
footnote: text[^1] and, later, [^1]: the note note at the end ⚠ the text of the note disappears in the PDF; write the remark in the text itself or in a ## Notes section

3. Images

Add

Put the file in docs/, next to enunciado.md, and refer to it by its name:

![Map of the cities](mapa.png)

(You can also paste or drag the image into the web editor. The editor then embeds it in the text.) Use simple names (letters, digits, ., _, -). The formats are png, jpg, jpeg, gif, svg or webp, up to 2 MB. The PACOTE document gives the details.

The text between the brackets decides the result:

You write Result
![Map of the cities](mapa.png) alone in the paragraph figure: the image and, below it, the caption "Map of the cities" (in italics in the PDF)
![](mapa.png) alone in the paragraph the image, without a caption
… the symbol ![](seta.png) indicates … in the middle of a sentence the image inside the line β€” only for small icons

Size

If you specify nothing, the image comes out at its natural size. It does not go past the width of the text (on the site) or of the page (in the PDF). To set the size, give the width in % of the text width:

![Map of the cities](mapa.png){width=50%}

This applies on the site and in the PDF. The height follows (the aspect ratio stays the same).

Center

By default, the image (and the figure) is on the left. To center it, put it inside a ::: center block, with a blank line before the block:

The network looks like this:

::: center
![Map of the cities](mapa.png){width=60%}
:::

This applies on the site (the problem page, the contest, the HTML tab and the editor's πŸ‘ Preview) and in the PDF. The caption is centered too. The block can contain more than one image, and also text. (The command-line moj preview still shows the block on the left.)

Does not work Why
![Map](mapa.png){.center} a class on the image does not center it (.center is for the block)
<center>…</center> or <div style="text-align:center">…</div> raw HTML: it centers on the site, but not in the PDF

Graph

To draw a graph from DOT instead of a pasted image, see mojtools/docs/enunciado-grafos.md.

4. Formulas: the rules

5. Formulas: the cheat sheet

Each row gives what you write, how it comes out on the site, and how it comes out in the PDF of the problem set.

Subscripts, powers and constraints

You write Result PDF
$a_i$, $a_{i,j}$, $x_i^2$ aia_i, ai,ja_{i,j}, xi2x_i^2 βœ“
$x^2$, $2^{n+1}$, $10^{18}$ x2x^2, 2n+12^{n+1}, 101810^{18} βœ“
$1 \le N \le 2 \cdot 10^5$ 1≀N≀2β‹…1051 \le N \le 2 \cdot 10^5 βœ“
$0 \le a_i < 2^{63}$ 0≀ai<2630 \le a_i < 2^{63} βœ“
$a \ne b$, $a \ge b$, $a \approx b$ aβ‰ ba \ne b, aβ‰₯ba \ge b, aβ‰ˆba \approx b βœ“
$10^9 + 7$ 109+710^9 + 7 βœ“
values $\le 10^9$ (relation without the left side) values ≀109\le 10^9 βœ“

Fractions, roots, sums

You write Result PDF
$\frac{a}{b}$ ab\frac{a}{b} βœ“
$\dfrac{a}{b}$ (larger, inline) ab\dfrac{a}{b} βœ“
$\sqrt{x}$, $\sqrt[3]{x}$ x\sqrt{x}, x3\sqrt[3]{x} βœ“
$\sum_{i=1}^{n} a_i$, $\prod_{i=1}^{n} a_i$ βˆ‘i=1nai\sum_{i=1}^{n} a_i, ∏i=1nai\prod_{i=1}^{n} a_i βœ“
$\max_{1 \le i \le n} a_i$, $\min(a, b)$ max1≀i≀nai\max_{1 \le i \le n} a_i, min(a,b)\min(a, b) βœ“
$\binom{n}{k}$ (nk)\binom{n}{k} βœ“

Functions, complexity, arithmetic

You write Result PDF
$\log n$, $\log_2 n$, $\ln x$ logn\log n, log2n\log_2 n, lnx\ln x βœ“
$\gcd(a, b)$, $\operatorname{lcm}(a, b)$ gcd(a,b)\gcd(a, b), lcm(a,b)\operatorname{lcm}(a, b) βœ“
$O(n \log n)$, $O(n^2)$, $\mathcal{O}(1)$ O(nlogn)O(n \log n), O(n2)O(n^2), π’ͺ(1)\mathcal{O}(1) βœ“
$a \bmod m$, $a \equiv b \pmod{m}$ amodma \bmod m, a≑b(modm)a \equiv b \pmod{m} βœ“
$\lfloor n / 2 \rfloor$, $\lceil n / k \rceil$ ⌊n/2βŒ‹\lfloor n / 2 \rfloor, ⌈n/kβŒ‰\lceil n / k \rceil βœ“
$a \times b$, $a \cdot b$, $a \pm b$ aΓ—ba \times b, aβ‹…ba \cdot b, aΒ±ba \pm b βœ“
$n!$, $-x$ n!n!, βˆ’x-x βœ“
$\infty$ ∞\infty βœ“

Sets, logic, arrows, Greek

You write Result PDF
$\{1, 2, \ldots, n\}$ {1,2,…,n}\{1, 2, \ldots, n\} βœ“
$a_1, \dots, a_n$, $a_1 + \cdots + a_n$ a1,…,ana_1, \dots, a_n, a1+β‹―+ana_1 + \cdots + a_n βœ“
$x \in S$, $x \notin S$, $A \subseteq B$ x∈Sx \in S, xβˆ‰Sx \notin S, AβŠ†BA \subseteq B βœ“
$A \cup B$, $A \cap B$, $\emptyset$ AβˆͺBA \cup B, A∩BA \cap B, βˆ…\emptyset βœ“
$\mathbb{Z}$, $\mathbb{N}$, $\mathbb{R}$ β„€\mathbb{Z}, β„•\mathbb{N}, ℝ\mathbb{R} βœ“
$\{x \in S \mid x > 0\}$ (use \mid, not |) {x∈S∣x>0}\{x \in S \mid x > 0\} βœ“
$a \land b$, $a \lor b$, $\neg a$, $a \oplus b$ a∧ba \land b, a∨ba \lor b, Β¬a\neg a, aβŠ•ba \oplus b βœ“
$\forall x$, $\exists y$ βˆ€x\forall x, βˆƒy\exists y βœ“
$a \to b$, $a \Rightarrow b$, $a \iff b$ aβ†’ba \to b, aβ‡’ba \Rightarrow b, a⇔ba \iff b βœ“
$\alpha$, $\beta$, $\pi$, $\varepsilon$, $\Delta$, $\Sigma$ Ξ±\alpha, Ξ²\beta, Ο€\pi, Ξ΅\varepsilon, Ξ”\Delta, Ξ£\Sigma βœ“

Text and space inside the formula

You write Result PDF
$\text{if } x > 0$ (normal text inside the formula) if x>0\text{if } x > 0 βœ“
$a\,b$ (thin space), $a \quad b$ (wide space) aba\,b, aba \quad b βœ“
$\mathbf{v}$, $\mathrm{d}x$ 𝐯\mathbf{v}, dx\mathrm{d}x βœ“
$\texttt{abc}$ πšŠπš‹πšŒ\texttt{abc} ⚠ a different monospaced font; for strings, use `abc` outside the formula
$50\%$, $a \# b$, $a \& b$ 50%50\%, a#ba \# b, a&ba \& b βœ“

Where the PDF is different

You write Result PDF
$\bar{x}$, $\hat{x}$, $\vec{v}$, $\tilde{x}$, $\dot{x}$ xβ€Ύ\bar{x}, xΜ‚\hat{x}, vβ†’\vec{v}, xΜƒ\tilde{x}, xΜ‡\dot{x} ⚠ the accent comes out detached, high and small
$\overline{AB}$, $\underline{x}$ AB¯\overline{AB}, x_\underline{x} ⚠ the same
$f'(x)$, $f''(x)$ fβ€²(x)f'(x), fβ€³(x)f''(x) ⚠ the prime (β€²) comes out small and far from the letter

If the PDF is important, do not use a name with an accent. Use a different name ($m$ for the mean instead of $\bar{x}$, $g$ instead of $f'$). On the site, everything comes out correctly.

6. Parentheses, brackets and bars

Write them as in TeX. The size is set automatically:

7. Structures: cases, matrices, alignment

Definition by cases:

$$f(n) = \begin{cases} 1 & \text{if } n = 0 \\ 2 f(n-1) & \text{if } n > 0 \end{cases}$$

f(n)={1if n=02f(nβˆ’1)if n>0f(n) = \begin{cases} 1 & \text{if } n = 0 \\ 2 f(n-1) & \text{if } n > 0 \end{cases}

Matrix (pmatrix between parentheses, bmatrix between brackets, vmatrix between bars):

$$A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}, \qquad \det A = \begin{vmatrix} a & b \\ c & d \end{vmatrix}$$

A=(abcd),detA=∣abcd∣A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}, \qquad \det A = \begin{vmatrix} a & b \\ c & d \end{vmatrix}

Calculations aligned on the = (& marks the alignment point, \\ breaks the line):

$$\begin{aligned} S &= a_1 + a_2 + \cdots + a_n \\ &= \frac{n(n+1)}{2} \end{aligned}$$

S=a1+a2+β‹―+an=n(n+1)2\begin{aligned} S &= a_1 + a_2 + \cdots + a_n \\ &= \frac{n(n+1)}{2} \end{aligned}

All of them come out correctly in the PDF (βœ“).

8. What to avoid

Avoid Why Write
1 ≀ N ≀ 10⁡ typed as text in the PDF it comes out in a different font $1 \le N \le 10^5$
\textbf{…}, \emph{…}, \section{…}, \begin{itemize} this is LaTeX for text, not Markdown: the text disappears, without a warning, on the site and in the PDF **…**, *…*, ## …, - item
$ x $ with a space next to the $, it is not a formula $x$
a sample copied into the text it appears twice (the samples come from tests/) tests/input/sample1 + docs/notes/sample1.md
$\bar{x}$, $f'$ when the PDF is important see the table "Where the PDF is different" a different name
{…} to show braces in TeX, { only groups \{…\}
<center>, <div style=…> to center it does not center in the PDF ::: center (section 3)
footnote ([^1]) in the PDF, the text of the note disappears the remark in the text or in ## Notes

9. A complete statement

A store records the price $p_i$ of each of the $N$ days of a season. For an interval of
days $[l, r)$ β€” from day $l$ to day $r - 1$ β€”, the *profit* is
$$L(l, r) = \max_{l \le i < r} p_i - \min_{l \le i < r} p_i.$$

Given $Q$ queries, answer the profit of each interval. If $r - l < 2$, the profit is $0$.

## Input

The first line contains two integers $N$ and $Q$ ($1 \le N, Q \le 2 \cdot 10^5$). The second line
contains $N$ integers $p_1, p_2, \ldots, p_N$ ($1 \le p_i \le 10^9$). Each of the next $Q$ lines
contains two integers $l$ and $r$ ($1 \le l < r \le N + 1$).

## Output

For each query, print a line with $L(l, r)$.

## Subtasks

| Subtask   | Points | Constraints               |
|:---------:|:------:|---------------------------|
| 1         | 30     | $N, Q \le 1000$           |
| 2         | 70     | no additional constraints |

10. How to check


For the MOJ maintainers: the PDF of the problem set comes from pandoc -f html -t odt β†’ soffice. LibreOffice does not draw the MathML. It translates the MathML to StarMath and reads that text. server/api/v1/lib/odt-math-bars.py fixes the MathML between the two steps (the size and font of the formula, the delimiters, the syntax characters, a relation without an operand, and also the size of the images). server/api/v1/lib/odt-center.lua centers the ::: center block (the Center style of server/etc/caderno-reference.odt). The PDF column of this page was measured with pandoc 3.1.11 and LibreOffice 25.2 (the versions in the image). The tests that support it are server/test/smoke-odt-math-bars.sh and server/test/render-docs.sh.