PivotOJ

Polyomino Tiling

시간 제한: 5000ms메모리 제한: 2048MB출처: ICPC ECNA 2025-2026BOJ 35382

문제

A polyomino is a nonempty connected union of unit squares without holes, where the corners of each unit square are at integer coordinates in the two-dimensional plane. A polyomino can be described by a string representation of its boundary. Starting at some integer coordinates on the polyomino's boundary we can follow the boundary by taking unit steps up, right, down, or left. We represent each of these steps with the letters u, r, d, l, respectively. If we concatenate these letters while following the boundary of the polyomino until we reach the starting coordinates, then the concatenation is called the boundary string for the polyomino. Note that while following the boundary, only the starting coordinates may be visited twice, all other coordinates in the path must be visited exactly once.

The cyclic rotations of the boundary string are obtained by starting at different coordinates on the boundary and proceeding in the same order as in the boundary string (either clockwise or counter-clockwise, but not both). For example, the cyclic rotations of urdl are urdl, rdlu, dlur, and lurd.

For a string SS, consisting of letters u, r, d, and l, define S\overline{S} as the string obtained by reversing the letters of SS and changing each u to d, each d to u, each r to l, and each l to r. For example, for S=S=uruurrdl, we have S=\overline{S}=rullddld.

It can be proven that a polyomino will tile the plane by translations (i.e., without rotations nor reflections) if and only if a cyclic rotation of the boundary string BB can be written as B=XYZXYZB=X\cdot Y\cdot Z \cdot \overline{X} \cdot \overline{Y} \cdot \overline{Z} or B=XYXYB=X\cdot Y \cdot \overline{X}\cdot \overline{Y}, where X,Y,ZX,Y,Z are nonempty strings. Quite often, there may be many ways to rotate the boundary string and write it in one or both of these two forms.

Given a boundary string for a polyomino, count the number of ways that each cyclic rotation BB of the boundary string can be written as either B=XYXYB=X\cdot Y \cdot \overline{X} \cdot \overline{Y} or B=XYZXYZB=X\cdot Y \cdot Z\cdot \overline{X} \cdot \overline{Y} \cdot \overline{Z} where XX, YY, and ZZ are nonempty strings. This count will be 00 if the polyomino cannot tile the plane by translations.

입력

Input consists of a single line containing two values kk ss, where kk (4k100004\leq k\leq 10\,000) is the length of the boundary string and ss is the boundary string. The boundary string will consist of the letters u, r, d, and l.

출력

Print a single integer, the number of ways that each cyclic rotation of the boundary string can be written in the form XYXYX \cdot Y \cdot \overline{X} \cdot \overline{Y} or XYZXYZX \cdot Y \cdot Z \cdot \overline{X} \cdot \overline{Y} \cdot \overline{Z}.

힌트

Part of a tiling for the first polyomino in the sample input.

예제

예제 1

입력
20 uurrrrdrdrdlldlullul
출력
6

예제 2

입력
14 ururdrrdldllul
출력
4

예제 3

입력
12 uurdrurddlll
출력
0

예제 4

입력
8 urrrdlll
출력
16
코드를 제출하려면 로그인하세요.