G. Cutie Pie

Codeforces
IDCF10102G
Time1000ms
Memory64MB
Difficulty
English · Original
Formal · Original
Consider a NxM small-letters grid. SVU asked you to check whether this grid is a Cutie Pie or not A grid is a cutie pie if you can find the word "pie" in any direction (vertical, horizontal, and radial). Your program should output "Cutie Pie!" if the grid contains the word "pie" or "Sorry Man" otherwise The first line contains T 1<=T<=10000 the number of test cases. The followed T lines represent the test cases, each one contains two integers 0 < N,M  ≤  20 then N lines each of them contains M English small-letter separated by space characters. There is a blank line between each two successive test cases. For each test case output "Cutie Pie!" if the grid in the test case contains the word "pie" or "Sorry Man" otherwise. ## Input The first line contains T 1<=T<=10000 the number of test cases. The followed T lines represent the test cases, each one contains two integers 0 < N,M  ≤  20 then N lines each of them contains M English small-letter separated by space characters. There is a blank line between each two successive test cases. ## Output For each test case output "Cutie Pie!" if the grid in the test case contains the word "pie" or "Sorry Man" otherwise. [samples]
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case, let $ h, w, d \in \mathbb{Z} $ denote: - $ h $: height of the tower (number of floors), - $ w $: width of the tower (number of rooms per floor), - $ d $: room index (1-based) on the ground floor (floor 1) where the monster is located. The hero starts at position $ (h, 1) $ (top-left room). The stone moves diagonally down-right until it hits the right border ($ w $), then diagonally down-left until it hits the left border ($ 1 $), and so on, alternating direction upon hitting a vertical border. The stone stops when it reaches floor 1 (ground floor). **Constraints** 1. $ 1 \leq T \leq \text{unknown} $ (but input size implies practical limits) 2. $ 2 \leq h, w \leq 10^9 $ 3. $ 1 \leq d \leq 10^9 $ **Objective** Determine whether the stone, starting at $ (h, 1) $ and moving diagonally with reflection at vertical boundaries, lands exactly on room $ d $ of floor 1. Define the stone’s horizontal position as a function of floor $ f $ (from $ h $ down to 1). Let $ x(f) $ be the horizontal position (1-based) of the stone when it reaches floor $ f $. The motion is periodic with period $ 2(w - 1) $ in horizontal displacement per vertical drop of $ w - 1 $ floors. The stone moves $ \Delta x = +1 $ per floor down while going right, and $ \Delta x = -1 $ per floor down while going left. The horizontal displacement from top to bottom is equivalent to simulating a reflection path: - The stone’s horizontal trajectory modulo $ 2(w - 1) $ determines its final position. Let $ \text{total\_vertical\_steps} = h - 1 $. The horizontal displacement is $ \Delta = h - 1 $. The stone’s horizontal position on floor 1 is given by: $$ x(1) = 1 + \Delta \mod 2(w - 1) $$ But if the result exceeds $ w $, reflect: Let $ r = (h - 1) \mod (2(w - 1)) $. Then: - If $ r < w $, then $ x(1) = 1 + r $ - Else, $ x(1) = 2w - 1 - r $ Alternatively, use the standard reflection formula: $$ x(1) = 1 + \left| (h - 1) \mod (2(w - 1)) - (w - 1) \right| $$ But simpler: Let $ r = (h - 1) \mod (2(w - 1)) $ Then: $$ x(1) = \begin{cases} 1 + r & \text{if } r < w \\ 2w - 1 - r & \text{if } r \geq w \end{cases} $$ **Objective (Formal)** For each test case, compute $ x(1) $ as above. Output "Yes" if $ x(1) = d $, otherwise "No".
API Response (JSON)
{
  "problem": {
    "name": "G. Cutie Pie",
    "description": {
      "content": "Consider a NxM small-letters grid. SVU asked you to check whether this grid is a Cutie Pie or not  A grid is a cutie pie if you can find the word \"pie\" in any direction (vertical, horizontal, and radi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 65536
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10102G"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Consider a NxM small-letters grid. SVU asked you to check whether this grid is a Cutie Pie or not  A grid is a cutie pie if you can find the word \"pie\" in any direction (vertical, horizontal, and radi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case, let $ h, w, d \\in \\mathbb{Z} $ denote:  \n- $ h $: height of the tower (number of floors),  \n- $ w $: width...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments