ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Two Nonattacking Knights

Count safe placements of two knights for each square board size.

สิ่งที่คุณจะได้เรียนรู้

  • Count all placements with combinations
  • Subtract the attacking positions
  • Derive a closed formula for each board size

โจทย์

ดูฉบับแปล

For each board size k from 1 through n, count unordered pairs of distinct squares on a k by k board where two knights cannot attack each other. A knight moves two squares in one axis and one in the other.

. x . x .
x . . . x
. . K . .
x . . . x
. x . x .

K marks a knight and x its possible destinations, when those squares lie on the board. Swapping the two knights does not create another placement.

Implement two_knights(n).

Input

n: The largest board size to consider.

Output

Return n counts in order, for board sizes 1 through n.

เงื่อนไข

1 ≤ n ≤ 10,000. Counts fit signed 64-bit integers.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 3

ผลลัพธ์ที่คาดหวัง:
expected = [0, 6, 28]
อินพุต:
long long n = 3;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {0, 6, 28};
อินพุต:
long n = 3;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {0, 6, 28};

คำอธิบาย

A 1 × 1 board cannot hold two knights. A 2 × 2 board has six pairs and no attacks. A 3 × 3 board has 36 pairs, of which eight attack, leaving 28.

ตัวอย่างที่ 2
อินพุต:
n = 1

ผลลัพธ์ที่คาดหวัง:
expected = [0]
อินพุต:
long long n = 1;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {0};
อินพุต:
long n = 1;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {0};

คำอธิบาย

Two distinct squares are required, but the board has only one.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Start with all unordered pairs of squares on each board, then subtract the pairs where knights attack.

คำใบ้ 2

Only a small, fixed number of knight attack placements fit inside any 2 by 3 or 3 by 2 rectangle.

คำใบ้ 3

Edge boards like 1 by 1 and 2 by 2 need to naturally produce zero attacking pairs.

ความซับซ้อนที่คาดหวัง

O(n) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก