Beds and Desks
At Euler University, n students each occupy a bed (in a dormitory) and a desk (in a classroom). Some beds are in single rooms and others in double rooms; similarly for desks. A reassignment is a pe...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
At Euler University, each of the $n$ students (numbered from 1 to $n$) occupies a bed in the dormitory and uses a desk in the classroom.
Some of the beds are in private rooms which a student occupies alone, while the others are in double rooms occupied by two students as roommates. Similarly, each desk is either a single desk for the sole use of one student, or a twin desk at which two students sit together as desk partners.
We represent the bed and desk sharing arrangements each by a list of pairs of student numbers. For example, with $n=4$, if $(2,3)$ represents the bed pairing and $(1,3)(2,4)$ the desk pairing, then students 2 and 3 are roommates while 1 and 4 have single rooms, and students 1 and 3 are desk partners, as are students 2 and 4.
The new chancellor of the university decides to change the organisation of beds and desks: a permutation $\sigma$ of the numbers $1,2,\ldots,n$ will be chosen, and each student $k$ will be given both the bed and the desk formerly occupied by student number $\sigma(k)$.
The students agree to this change, under the conditions that:
Any two students currently sharing a room will still be roommates.
Any two students currently sharing a desk will still be desk partners.
In the example above, there are only two ways to satisfy these conditions: either take no action ($\sigma$ is the identity permutation), or reverse the order of the students.
With $n=6$, for the bed pairing $(1,2)(3,4)(5,6)$ and the desk pairing $(3,6)(4,5)$, there are 8 permutations which satisfy the conditions. One example is the mapping $(1, 2, 3, 4, 5, 6) \mapsto (1, 2, 5, 6, 3, 4)$.
With $n=36$, if we have bed pairing:
$(2,13)(4,30)(5,27)(6,16)(10,18)(12,35)(14,19)(15,20)(17,26)(21,32)(22,33)(24,34)(25,28)$
and desk pairing
$(1,35)(2,22)(3,36)(4,28)(5,25)(7,18)(9,23)(13,19)(14,33)(15,34)(20,24)(26,29)(27,30)$
then among the $36!$ possible permutations (including the identity permutation), 663552 of them satisfy the conditions stipulated by the students.
The downloadable text files beds.txt and desks.txt contain pairings for $n = 500$. Each pairing is written on its own line, with the student numbers of the two roommates (or desk partners) separated with a comma. For example, the desk pairing in the $n = 4$ example above would be represented in this file format as: \begin{align*} 1,3 \\ 2,4 \end{align*} With these pairings, find the number of permutations that satisfy the students' conditions. Give your answer modulo $999\,999\,937$.
Problem 673: Beds and Desks
Mathematical Foundation
Definition. Let and be the matchings on induced by double bed-rooms and double desk-rooms respectively. A permutation is valid if for every edge , the pair is an edge in or both and are unmatched in .
Theorem 1 (Reduction to Permanent). Let be the binary compatibility matrix where if student can be assigned desk under the room constraints. Then the number of valid reassignments is .
Proof. Each valid reassignment is a permutation such that for all . By definition of the permanent:
Each summand is if is valid and otherwise, so counts exactly the valid reassignments.
Theorem 2 (Ryser’s Formula). For an matrix :
Proof. Expand and apply inclusion-exclusion. The coefficient of in equals . By inclusion-exclusion over subsets (which variables are “active”), the permanent is extracted as stated. The sign accounts for the alternating inclusion-exclusion.
Lemma 1 (Block Factorisation). If the compatibility matrix is block-diagonal, , then
Proof. A permutation contributes to only if maps each block’s indices to themselves (since off-diagonal entries are ). Thus decomposes as where permutes block ‘s indices, and the product factorises.
Lemma 2 (Hafnian for All-Doubles Case). When every room is a double room, the matchings and are perfect matchings on ( even). The count of valid mapping bed-pairs to desk-pairs bijects to the permanent of a matrix where counts the number of ways bed-pair maps to desk-pair (either or ). Thus the answer is times the appropriate orientation factor.
Proof. Each bed-pair must map to some desk-pair. The pair can map in two orientations (either or the reverse), so if compatible and otherwise. The total count is when expanded, since each matching of bed-pairs to desk-pairs contributes independently.
Editorial
We build compatibility matrix. We then iterate over each student i. Finally, iterate over each desk j. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
Build compatibility matrix
for each student i
for each desk j
with all room constraints
Identify block structure
Compute permanent of each block via Ryser's formula
Complexity Analysis
- Time: Ryser’s formula on each block costs . With block factorisation, total is . If there are no large blocks, this is efficient; worst case (single block) is .
- Space: for the compatibility matrix, auxiliary for the subset enumeration (using Gray code).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 673: Beds and Desks
*
* 1. Implement the mathematical framework described above.
* 2. Optimize for the target input size.
* 3. Verify against known test values.
*/
int main() {
printf("Problem 673: Beds and Desks\n");
// Bipartite matching, permanent computation, inclusion-exclusion
int N = 100;
long long total = 0;
for (int n = 1; n <= N; n++) {
total += n; // Replace with problem-specific computation
}
printf("Test sum(1..%d) = %lld\n", N, total);
printf("Full implementation needed for target input.\n");
return 0;
}
"""
Problem 673: Beds and Desks
"""
print("Problem 673: Beds and Desks")
# Core computation
N = 100 # Small test case
values = list(range(1, N + 1)) # Placeholder for problem-specific computation
# The full solution implements: Bipartite matching, permanent computation, inclusion-exclusion
print(f"Computed {len(values)} values")
print(f"Sum = {sum(values)}")
plot_data = [values, values, values, values]