#SP0000142. Bảng không có hiệu nguyên tố (No Prime Differences)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bảng không có hiệu nguyên tố (No Prime Differences)
No Prime Differences
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers , fill an grid with the integers from to , using each number exactly once.
Two cells are adjacent if they share a side. For every adjacent pair of cells, the absolute difference between their values must not be prime.
A valid arrangement is guaranteed to exist. Print any valid arrangement.
Input
One line contains integers .
Output
Print rows of integers. Every value from to must appear exactly once, and every side-adjacent absolute difference must be nonprime.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
4 4
Output
5 6 7 8
13 14 15 16
1 2 3 4
9 10 11 12
Explanation
The grid uses each number from to once. Horizontal neighbors differ by , which is not prime. Vertical neighbors between consecutive rows differ by , , and , all composite. Thus the grid is valid; other arrangements are allowed.