Ctrl K

Prime Guests in Prime Seats

Easy
A row has n seats numbered 1 to n, and there are n guests numbered 1 to n. Every guest gets one seat, so that a guest with a prime number sits in a seat with a prime number, and a guest with a non-prime number sits in a seat with a non-prime number. Recall that a prime is greater than 1 and divisible only by 1 and itself; 1 is not prime. Return the number of such seatings. It can be huge, so return it modulo 10⁹ + 7.
Example 1
n=4 → 4

The prime seats are 2 and 3. Guests 2 and 3 can take them in 2 ways, and guests 1 and 4 can take seats 1 and 4 in 2 ways: 2 · 2 = 4.

Example 2
n=1 → 1

One guest and one seat give exactly one seating.

Example 3
n=100 → 682289015
Constraints
  • 1 ≤ n ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Number That Reads Both Ways EasyValue of a Roman Numeral EasyAdd One to a Digit Array EasySum of Binary Strings EasyWhole Square Root Easy
loading Python…
Input
n=4
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 30 tests on the server, hidden ones included.