We are given n (number of students), k (number of buses, unlimited capacity), d (number of days). We need to output for each day, an assignment of each student to a bus (1..k) such that: No two students become "close friends", i.e., no two distinct students are assigned to the same bus on all d days. In other words, for any pair of distinct students, there exists at least one day where they are in different buses.
We need to check if it's possible, and if yes, output d lines, each with n integers (the bus assignments). Buses are unlimited capacity so each bus can hold any number of students.
Essentially, we want to assign each student a vector of length d with entries from {1,...,k} such that all vectors are distinct. Because if two students have identical assignments across all days, then they are close friends. Conversely, if they have at least one different day, they are not close friends. So the condition is exactly that the d-dimensional vectors (bus numbers for each day) assigned to the n students must be all distinct. That's it.
Thus, the problem reduces to: We need to assign to n students distinct sequences (strings) of length d over an alphabet of size k (buses numbered 1..k). The total number of possible distinct sequences is k^d. So we need n <= k^d. If n > k^d, it's impossible -> output -1. Otherwise, we need to produce any valid assignment: d lines each listing n numbers: the i-th line corresponds to day i, and the j-th integer is the bus for student j on that day. So we need to output a matrix of size d x n, where each column (student) is a distinct vector.
One simple way: Enumerate the n distinct vectors in some order, maybe using base-k representation of numbers from 0 to n-1 (or something). Use digits from 0 to k-1, add 1 to each to get bus numbers 1..k. But careful: we need to output per day, so each row i is the i-th digit for all columns.
Thus, we can generate n distinct sequences of length d over alphabet size k. Since n <= k^d, we can use lexicographic order: assign students numbers from 0 to n-1, convert to base k with d digits (padding leading zeros). Then for each day (digit position from most significant to least or vice versa), output the digit+1 for each student.
But we must ensure that sequences are distinct. That's satisfied.
Thus solution: if n > k^d, print -1. Else: For each student j from 0 to n-1, compute its representation in base k with d digits. Typically we can compute digits by repeatedly dividing by k. Or simply iterate number from 0 to n-1, for each compute digits. Since n,d <= 1000, and we have up to 1000*1000 = 10^6 entries, that's fine.
But k can be up to 1e9, which is large, so we can't precompute an array of size k. But we only need to compute digits for numbers up to n-1 which is at most 1000, so it's fine. However k^d could be huge; we need to check if n <= k^d. Since both n,d <= 1000, but k^d may overflow. We can compute by repeated multiplication while checking > n, or use logarithms. Since n <= 1000, we can just compute power with early exit: result=1; for i=1..d: result *= k; if result >= n then break; but careful about overflow: if k is large and d large, result can overflow 64-bit? But we can break early if result > n (since we only care about compare to n). Use Python's big integers? Actually we are implementing in C++? The problem statement is from Codeforces, typical solution in C++ uses long long, but k up to 1e9, d up to 1000 => k^d could be astronomically huge, far beyond 64-bit. But we only need to check if it's >= n, and n <= 1000, so we can break as soon as result >= n, or if result overflows we could also treat as >= n? But overflow in C++ would be problematic. We can handle by: if k==1, then k^d =1, so only possible if n<=1. If k>=2, then k^d grows quickly, so for d up to 1000, k^d will exceed 1000 after at most 10 steps (since 2^10=1024). So we can compute using loop with checking: if result > n / k (to avoid overflow) before multiplying? Or use long double. Simpler: if k==1: then only possible if n==1, else impossible. Else, we can compute pow(k,d) safely as: result=1; for i=1..d: if result > n/k) then result = n+1; break; else result = k; After loop, compare result with n. Since n <= 1000, this works because n/k might be zero if k > n? But careful: when result > n/k, then resultk > n, so we can set result = n+1 and break. That ensures we don't overflow.
Alternatively, we can avoid the power check entirely by a combinatorial argument: we need to assign distinct sequences. If d >= log_k n, i.e., if we can represent n distinct codes with d digits base k. Since n <= 1000, we can check if d * log10(k) >= log10(n). But using integer arithmetic is safer.
But note: k could be 1, then k^d = 1, so only n=1 works.
Now, after confirming possibility, we need to output d lines each with n bus numbers. The assignment we generate using base-k representation with digits from 0 to k-1. But careful: we have students numbered 1..n maybe 0-index. And we need to output in order of student index: the j-th integer of i-th line is the bus for student j (presumably student j is the j-th student we assign). So we can assign student 1 the sequence corresponding to number 0, student 2 number 1, etc.
Now, we need to output digits from day 1 to day d. Which digit corresponds to which day? The representation: For number x, we can compute digits for positions: day 1 could be the least significant or most significant; it doesn't matter as long as they are consistent and distinct. Typically, we can treat day 1 as the least significant, but any order works. However, note that we want each column vector to be distinct, and the assignment of digits to days is arbitrary. So we can choose a convention.
Simplify: For each student index s from 0 to n-1, we compute an array a[d] where a[0] is the "first day" bus number. We can fill a by: tmp = s; for i in range(d): a[i] = (tmp % k) + 1; tmp //= k; This would assign day 1 as the least significant digit, day 2 as next, etc. That's fine. Or we could assign day 1 as most significant. It doesn't matter. But we need to output lines per day. So we can store a 2D array assignments: days x students, or better to store per day strings. Since d and n <= 1000, we can allocate a vector<vector> ans(d, vector(n)). Then for s=0..n-1, compute digits and fill ans[day][s] = digit+1.
Alternatively, we can directly output row by row without storing all: We could precompute all numbers' digits for each day separately, but storing is fine.
Edge Cases:
- n=1 always possible (any k>=1, d any). But need to output d lines each with "1". Because student 1 in bus 1 always; distinctness condition trivially satisfied.
- k=1, n>1: impossible (since only one bus, all students would be in same bus every day -> close friends). But check: n>1, k=1 => k^d =1 < n => impossible -> output -1.
- d=1: condition reduces to: each student must be in distinct bus? Wait, d=1: close friends if they are in same bus on the only day. Actually definition: two students become close friends if and only if they are in the same buses for all d days. For d=1, that means if they are in same bus on that day, they become close friends. So we need to avoid any two students being in the same bus on that day. So we need each student assigned to a distinct bus. Since there are k buses, we need n <= k. If n <= k, we can assign each student a distinct bus. But using our distinct sequences approach: d=1, sequences are just bus numbers, and they need to be distinct. So n <= k^1 = k. So same condition. So for d=1, we need to output 1 line where each student gets a distinct bus number. Our base-k representation with d=1: each student s gets bus number (s % k) +1, but that would assign many students same bus if n>k? Actually if n>k, we would have repeated bus numbers because we only have k distinct digits. But we only execute if n <= k, so s from 0..n-1 are all < k, so s%k = s, giving distinct numbers. So good.
Check sample: n=3,k=2,d=2: k^d = 4 >= 3, possible. Our method: students s=0,1,2. Compute digits:
s=0: base2 digits (2 digits) least significant first? Day1 = (0%2)+1=1, 0//2=0 => Day2 = (0%2)+1=1 => sequence (1,1). s=1: Day1= (1%2)+1=2, 1//2=0 => Day2=1 => (2,1). s=2: Day1= (2%2)+1=1, 2//2=1 => Day2= (1%2)+1=2 => (1,2). Output rows: day1: 1 2 1; day2: 1 1 2. That's transpose of sample? Sample output is:
1 1 2
1 2 1
In sample: day1: 1 1 2; day2: 1 2 1. So each student's vector: student1: (1,1); student2: (1,2); student3: (2,1). Distinct vectors. Our assignment gives student1:(1,1), student2:(2,1), student3:(1,2). That's also distinct, just different ordering of students. That's acceptable because students are labeled; any arrangement that yields distinct vectors works. So our output would be:
1 2 1
1 1 2
That is also valid. But we must check ordering: The sample output seems to have student1: bus1 day1, bus1 day2; student2: bus1 day1, bus2 day2; student3: bus2 day1, bus1 day2. That's same set of vectors but assignment to students different. Both are valid. So our method is fine.
But note: In sample output they have day1 line: "1 1 2", day2 line: "1 2 1". Our day1: "1 2 1", day2: "1 1 2". That's different but still correct. However, there is nuance: The problem statement: "print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day." So they require that the j-th student's assignments across days correspond to column j. So any valid assignment of distinct vectors to students is acceptable. So we can output our assignment. However, it's safer to follow perhaps the lexicographic order from most significant digit as day1, but it's not required.
Nevertheless, we might want to output exactly as sample? No, any valid arrangement is accepted. So our method is good.
Now, we need to consider if d can be large up to 1000 and k can be large (like 1e9). When computing digits for each student using modulo and division by k, that's fine because k may be large but operations on integers are fine. But if k is huge, say k=1e9, d=1000, then k^d is astronomically huge, but we only need to represent numbers up to n-1 (<=999) in base k. That's trivial: the representation will have the first digit (least significant) as s % k, which is s (if s<k), and all higher digits zero. So effectively, the assignment for days beyond the first will be all 1's? Wait, careful: If we compute digits from least significant: for s in [0, n-1] with n <= k, then for d>1, the base-k representation of s requires d digits: we have s = a_0 + a_1k + a_2k^2+... Since s < k, we have a_0 = s, and all higher digits are 0. So days: day1 (least significant) = s+1, day2 = 1 (since 0+1), day3=1, ... dayd=1. That yields distinct vectors because day1 differ for each student (since s differ). That's valid. So it's fine.
Thus, algorithm:
- Read n, k, d.
- Check feasibility: if n > k^d: return -1.
Compute pow = 1; for i in 1..d:
if pow > n / k: pow = n+1; break;
pow *= k;
After loop, if n > pow: output -1; else continue.
But careful: need to handle k=0? k is at least 1 per constraints (1 ≤ k ≤ 1e9). So ok.
Alternative: if k == 1:
if n > 1: output -1; else output d lines of "1".
Else:
compute max_possible = 1; for i=0;i<d;i++) { if max_possible >= n) break; max_possible *= k; if max_possible > n) break; } Actually easier: we can loop up to d and if at any point max_possible >= n we can set possible true. Actually we need to ensure that we can have at least n distinct sequences. Since sequences are of length d, total distinct = k^d. So we need to compute if k^d >= n. With k>=2, it's enough to check if d >= ceil(log_k n). But to avoid overflow, we can compute iteratively: needed_digits = 0; temp = 1; while temp < n: temp *= k; needed_digits++; if needed_digits > d: impossible. That's simpler. So we can compute:
temp = 1;
for i in range(0,d):
if temp >= n: break;
temp *= k;
After loop, if temp < n: impossible else possible.
But careful: if k=1, then temp stays 1 always, so after d iterations, temp=1. So if n>1, impossible; else possible. So this loop works for all k>=1.
Alternatively:
bool possible = false;
long long cur = 1;
for (int i = 0; i < d; ++i) {
if (cur >= n) { possible = true; break; }
if (cur > n / k) { cur = n; break; } // to avoid overflow, but if cur already > n/k, next multiplication would exceed n, so we can set cur = n and continue? Actually we can break out after setting possible true? Let's think.
cur *= k;
}
if (cur >= n) possible = true;
Simpler: Use long double to compute pow but not needed.
We'll just do:
long long tot = 1;
for (int i = 0; i < d; i++) {
if (tot >= n) break; // already enough
if (tot > n / k) { // multiplication would exceed n, so set tot to n (or something) and break
tot = n;
break;
}
tot *= k;
}
if (tot >= n) feasible else not.
But careful: The loop condition is i<d. If after all d multiplications, tot may still be < n (if k=1 and n>1), so we need to check after loop.
Implementation:
long long tot = 1;
for (int i = 0; i < d; ++i) {
if (tot >= n) break;
if (tot > n / k) { // would overflow or just > n
tot = n; // ensure tot >= n? Actually if tot > n/k, then tot * k > n, so after multiplication, tot becomes > n. So we can set tot = n+1? But we can break out and treat as feasible.
// Actually we can just set tot = n+1; break; to indicate success.
tot = n+1;
break;
}
tot *= k;
}
if (tot >= n) possible = true; else possible = false;
But if k=1, then n/k = n, tot is 1, condition tot > n/k? 1 > n? false for n>1. So it will go through the loop d times, each time tot *= 1 => tot stays 1. After loop, tot=1 < n (if n>1) => impossible. Good.
If k>=2 and d large, after few steps tot will exceed n/k, and we set tot = n+1 and break, so feasible.
That works.
Now, if feasible, we produce output:
Initialize answer matrix: vector<vector> ans(d, vector(n));
for (int student = 0; student < n; ++student) {
int x = student; // number from 0
for (int day = 0; day < d; ++day) {
ans[day][student] = (x % k) + 1;
x /= k;
}
}
Then output: for each day i from 0 to d-1: for each student j: print ans[i][j] and space or newline after each line.
Note: The order of loops: day outer, student inner.
But this assigns least significant digit to day 0 (first day). That's fine.
However, consider the case where d is large, but n is small, and k is huge. For each student, we compute x%k. Since x < n <= 1000, and k could be > x, then x % k = x. So day0 gets values 0..n-1 +1 => 1..n. That's fine. And subsequent days: after dividing by k, x becomes 0 (since x<k), so all higher days get 1. So output is like:
Day1: 1 2 3 ... n
Day2: 1 1 1 ... 1
Day3: 1 1 1 ... 1
...
That's valid because only day1 distinguishes students.
But there is a nuance: The problem statement says "the j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day." So bus numbers must be between 1 and k. Our assignment yields bus numbers up to n (which <= 1000) and n may be <= k. But what if n > k? That case is impossible anyway. If n <= k, then bus numbers up to n are within 1..k, valid. Also later days all are 1, which is fine.
Thus algorithm is correct.
One more nuance: The condition "no two students become close friends if they are in the same buses for all d days." Our condition of distinct vectors is sufficient and necessary. So solution stands.
Time Complexity: O(d*n) up to 10^6 operations, fine.
Now, we need to implement in C++ (or other). We'll handle input and output.
Edge Cases:
- n=0? Not allowed, n>=1.
- d=0? Not allowed, d>=1.
Check Sample 2: n=3,k=2,d=1. Condition: n=3 > k^1=2 => impossible -> output -1. That matches.
Now, let's test with n=1,k=5,d=5: possible. Output: 5 lines each containing "1". That's fine.
n=1000, k=1000, d=1: possible. Need to output 1 line with numbers 1..1000? But our method: student s from 0..999 gives day0 = (s%k)+1 = s+1, distinct from 1 to 1000, valid.
n=1000, k=10, d=3: k^d=1000, exactly equal. Our method: for s from 0..999, base-10 representation with 3 digits (padded). This yields all distinct vectors. That's valid.
Now, note that we used least significant digit first. Could also use most significant first. It doesn't matter.
Potential issue: When k is large (like > 2^31), but we use int for modulo? In C++ we can use long long for k. But modulo of int (student index up to 999) by long long is fine. x % k where x is int and k is long long: promotion to long long, remainder computed. That's fine.
But we store ans as int; bus numbers could be up to k which is up to 1e9, which fits in 32-bit signed? 1e9 fits in int (2^31-1=2147483647). So int is fine.
Now, let's implement.
One more nuance: The condition for feasibility is n <= k^d, but we must also consider that if k == 0? No.
But we also need to consider that d might be huge (up to 1000) and k might be 1, then the loop will run d times (up to 1000) but it's okay.
Implementation:
#include
#include
#include
using namespace std;
int main() {
int n, d;
long long k; // up to 1e9
cin >> n >> k >> d;
// check feasibility
long long tot = 1;
for (int i = 0; i < d; ++i) {
if (tot >= n) break;
if (tot > n / k) { // multiplication would exceed n
tot = n; // set to n (or n+1) to indicate enough
break;
}
tot *= k;
}
if (tot < n) {
cout << -1 << endl;
return 0;
}
// produce assignment
vector<vector<int>> ans(d, vector<int>(n));
for (int student = 0; student < n; ++student) {
int x = student;
for (int day = 0; day < d; ++day) {
ans[day][student] = (x % k) + 1;
x /= k;
}
}
// output
for (int day = 0; day < d; ++day) {
for (int student = 0; student < n; ++student) {
cout << ans[day][student];
if (student != n-1) cout << " ";
}
cout << endl;
}
return 0;
}
But wait: In the feasibility loop, we set tot = n; break; to ensure tot >= n after break. However, if tot > n/k, then after multiplication we would get > n, but we haven't multiplied. So we can set tot = n+1; to guarantee >= n. Better: set tot = n; then after break tot < n? Actually if tot > n/k, then tot * k >= n+1? Possibly exactly n? Let's test: n=5, k=2, tot=3 (after some multiplications). n/k = 5/2 = 2, tot=3 >2, so tot*k =6 >=5. So we can set tot = n; or tot = n+1; Either way, tot >= n after break. So we can set tot = n; break; Then after loop we check if tot < n, but tot = n, so condition false. That works.
But careful: In case n/k is not integer, condition tot > n/k ensures tot*k > n. So tot becomes > n after multiplication, so we can set tot = n+1; but tot = n also works because we break and tot is not actually the multiplied value but we just need to know that after that multiplication it would be >= n. So we can treat it as feasible. So we can set a flag feasible = true when we detect such condition. Simpler: we can avoid this complication by using a loop that counts needed digits.
Alternative approach: Compute needed digits = 0; long long cur = 1; while (cur < n && needed_digits < d) { cur *= k; needed_digits++; } if (cur < n) impossible else possible.
But careful: For k=1, cur remains 1, needed_digits increments but cur never grows. This loop will continue d times, but we can break early if k==1 and cur < n: but we need to know that after d iterations, cur will still be 1, so after loop, if cur < n => impossible. So loop runs d times if k=1 and d may be up to 1000. That's acceptable. However, we should be cautious about overflow when cur * k may exceed 2^63-1, but we can break early when cur >= n. For k >= 2, cur grows quickly, and might overflow before reaching n? But we only need to know if cur >= n. So we can break early if cur > n / k to avoid overflow. Let's implement more systematically:
bool possible = false;
long long cur = 1;
for (int i = 0; i < d; ++i) {
if (cur >= n) { possible = true; break; }
if (cur > n / k) { // would exceed n after multiplication
possible = true; // because after multiply curk >= n+? Actually ensure curk > n? Yes because cur > n/k => curk > n. So it will become at least n+1? Actually if cur > n/k, then curk > n, so after multiplication we have cur >= n+? So it's possible. We can break.
break;
}
cur *= k;
}
if (!possible && cur >= n) possible = true;
But we need to handle case when loop completes without break: after d multiplications, cur may be < n (e.g., k=1, n>1) -> possible remains false.
Simplify: After loop, if cur >= n, possible else not.
But to avoid overflow, we need to be careful in multiplication. Use condition if cur > n / k then set cur = n (or something) and break.
Implementation:
long long cur = 1;
for (int i = 0; i < d; ++i) {
if (cur >= n) break;
if (cur > n / k) {
cur = n; // effectively set to n so after loop cur>=n
break;
}
cur *= k;
}
if (cur < n) { cout << -1; return 0; }
Works.
Now, note: For large k and d, this check is efficient.
But there is also the possibility that k^d might be less than n but due to our early break we set cur = n prematurely? Let's test: n=5, k=2, d=2: We need k^d = 4 < 5 => impossible. Let's run:
cur=1, i=0: cur < n (1<5), check cur > n/k? n/k=5/2=2, cur=1 >2? false, so cur *= k => cur=2.
i=1: cur=2 <5, check cur > n/k? 2 > 2? false (since not >), so cur *= k => cur=4.
Loop ends because i reached d=2. After loop, cur=4 <5 => impossible. Good.
n=5, k=2, d=3: k^d=8 >=5 => possible.
cur=1, i=0: cur<5, cur>n/k? 1>2? false => cur=2.
i=1: cur=2<5, cur>2? false => cur=4.
i=2: cur=4<5, cur>2? 4>2 true => cur = n = 5; break.
After loop cur=5 >=5 => possible. Good.
n=1000, k=1000, d=1: cur=1, i=0: cur<1000, cur > n/k? n/k=1, cur=1 >1? false, so cur *=1000 => cur=1000. Loop ends, cur=1000>=1000 => possible.
n=1000, k=1, d=1000: cur=1, i=0: cur<1000, cur > n/k? n/k =1000, cur=1>1000? false, cur*=1 => cur=1. Continue... after loop cur=1<1000 => impossible.
Edge: n=1, any k,d. cur=1, before loop or after? For d=0? d>=1. But cur starts at 1, i=0: cur>=n (1>=1) break, so cur=1, after loop check cur>=n => possible. So correct.
Thus feasibility check works.
Now, assignment: using modulo and division.
One nuance: When k is large (like > 2^31), x % k may cause x being int and k long long, but it's fine because int will be promoted to long long. However, we store the result in int, but k can be up to 1e9, which fits int. But k is long long in code, but modulo result is up to k-1, which can be up to 1e9, fits int. So we can cast to int.
But we need to be careful: In the assignment loop, x is int (since student index up to 1000). x % k yields a value up to k-1 (could be up to 1e9). That's fine to assign to int.
We'll use long long for k, but for ans we can use int, as 1e9 fits.
Now, output: d lines each with n numbers. For large d and n, output could be big, but 1000*1000 = 1e6 numbers, each up to 1e9, that's fine.
Now, ensure no trailing spaces: we can print spaces between numbers, and not after last number on each line.
Now, check sample: Input 3 2 2 -> Output per our method: Day1: student0: 1%2+1=2? Wait, student indices: 0-> day0:0%2+1=1? Actually careful: x = student. For student0: x=0, day0 (i=0): 0%2 = 0 => +1 = 1. Then x/=2 => 0. day1: 0%2=0 => +1=1. So student0: [1,1]. Student1: x=1: day0:1%2+1=2; x=0; day1:1 -> [2,1]. Student2: x=2: day0:0? 2%2=0 -> 1; x=1; day1:1%2+1=2 -> [1,2]. So rows: day0: [1,2,1]; day1: [1,1,2]. That's different from sample but valid.
Now, is there any scenario where this assignment fails because the bus numbers must be within 1..k? They are, since modulo yields 0..k-1, +1 yields 1..k.
Now, let's test a scenario: n=5, k=2, d=3 (possible). Our assignment: Students 0..4:
Compute base2 representation with 3 digits (LSB first):
0: 0 0 0 -> [1,1,1]
1: 1 0 0 -> [2,1,1]
2: 0 1 0 -> [1,2,1]
3: 1 1 0 -> [2,2,1]
4: 0 0 1 -> [1,1,2]
All distinct, good.
Now, does any student get bus number >k? No.
All good.
Thus the solution is straightforward.
But there is an alternative interpretation: Maybe the condition is that no two students become close friends, meaning that for any pair of students, there must exist at least one day where they are in different buses. Equivalent to all vectors distinct. So correct.
Now, what about the case where k^d is huge but n small; our assignment works.
Time Complexity O(n*d) which is at most 1e6.
Now, implement.
But wait: In the assignment loop, we used x as int, but for each student, we compute digits for d days by dividing by k. However, k might be 1. In that case, k=1, modulo operation yields 0, +1 = 1; division: x /= 1 keeps x unchanged; then the loop would produce all days with value 1 (since x never becomes 0). That's fine, but we also need distinct vectors? But k=1, n must be 1 for feasibility. So if n=1, student index 0, x=0, day0: 0%1 = 0? Actually 0 % 1 = 0, +1=1; x /=1 => 0; subsequent days: 0%1=0 -> 1. So vector all 1's. That's only one student, so distinct condition holds. So works.
But careful: If k=1 and n=1, feasible; output d lines of "1". Good.
Now, we must consider the possibility of n=0? Not allowed.
Now, test with n=1000, k=1000, d=1: we have ans[0][student] = (student % 1000) +1 = student+1, distinct, all within 1..1000.
Now, test with n=1000, k=10, d=3: tot = 10^3 =1000 exactly. Our assignment yields all 1000 distinct vectors. Output size: 3*1000 =3000 numbers, fine.
Now, check for n=1000, k=2, d=10: k^d=1024, feasible. Assignment: we convert numbers 0..999 to binary with 10 digits. That yields distinct vectors. Output: 10 lines of 1000 numbers each -> 10000 numbers, fine.
Thus implementation straightforward.
Now, possible pitfalls: The feasibility check could overflow if we don't guard. But we guard.
Edge: k=0? Not allowed.
Edge: n=1000, k=1e9, d=1000: tot quickly becomes > n, so we set cur = n and break, feasible. Assignment: since n <= k, each student gets unique day1 bus numbers from 1..n, and all other days bus=1. That's valid.
Now, is there any requirement that each bus must be used? No.
Thus solution complete.
Now, implement in C++.
We'll read integers:
long long n, k, d;
But n and d are up to 1000, so int is fine, but k up to 1e9, so long long.
But in feasibility, we need to compare n/k, so we need n as long long as well to avoid overflow in multiplication? Actually n is up to 1000, fits in int, but when computing n/k, if n is int and k long long, the division will promote to long long; fine. But to be safe, we can use long long for all.
Thus:
long long n, k, d;
cin >> n >> k >> d;
Now, feasibility:
long long cur = 1;
bool ok = false;
for (int i = 0; i < d; ++i) {
if (cur >= n) { ok = true; break; }
if (cur > n / k) { ok = true; break; }
cur *= k;
}
if (!ok && cur >= n) ok = true;
if (!ok) { cout << -1 << endl; return 0; }
But above check: after loop, if we broke due to cur > n/k, we set ok=true, but then we break. Actually we need to be careful to set ok only after checking. Alternatively, we can just compute final cur after loop with protection, and then check cur >= n.
Simplify:
long long cur = 1;
for (int i = 0; i < d; ++i) {
if (cur >= n) break;
if (cur > n / k) {
cur = n; // set to n to indicate enough
break;
}
cur *= k;
}
if (cur < n) {
cout << -1 << endl;
return 0;
}
Now, we proceed.
Now, assignment: We'll store ans as vector<vector>(d, vector(n)). But note: n and d are up to 1000, so 1e6 ints, memory ~4 MB, fine.
But we could avoid storing and compute on the fly for each day? Since we need to output rows, we could precompute for each student the representation and store columns. But storing is fine.
Alternatively, we can generate each day's row by iterating over students and computing the digit for that day without storing all. How? For each day i (0..d-1), we can compute for each student j the i-th digit (starting from LSB) as (j / (k^i)) % k + 1. But careful: When using power k^i, we need to compute pow = k^i, but k could be up to 1e9, and i up to 1000, k^i huge. But we can compute iteratively without overflow by using the fact that j is small (<=1000). Actually we could compute digits by repeated division as we did and store per student. But to avoid storage, we could compute each student's representation as a base-k number with d digits and store in a temporary array of size d for each student? That's same as storing.
Given constraints, storing is fine.
But we must note that we used ans[day][student] indexing. That might be convenient.
Now, we need to output each row. We can also output directly while computing. For each student, compute the digits and store in a 2D vector. Or we can compute all students' digits for all days and then output by days.
Given n,d <= 1000, it's fine.
Now, let's code.
Potential issue: When k is very large (like 1e9), and we do x % k, this is fine. But we also do x /= k; for x up to 999, after first division it becomes 0 because k > x, so division yields 0. So fine.
Now, test with sample:
Input:
3 2 2
cur check: cur=1, i=0: cur<3, cur>3/2? 1>1? false, cur=2; i=1: cur=2<3, cur>3/2? 2>1? true, set cur=3 break; cur=3>=3 => feasible.
assignment: as above, output:
1 2 1
1 1 2
But the sample output is:
1 1 2
1 2 1
Is there any requirement that the bus numbers should be exactly those? Probably not. However, we must check if the assignment meets the condition: For each pair of students, they must not be in same bus every day. Let's verify: Student 1 (col1): (1,1). Student 2 (col2): (2,1). Student 3 (col3): (1,2). All distinct: yes.
Now, sample expects output to be that, but any valid arrangement is accepted. So fine.
But perhaps there is a nuance: The problem statement says "Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity." So we just need to output any valid arrangement.
Now, test second sample: Input 3 2 1: cur=1, i=0: cur<3, cur>3/2? 1>1? false, cur*=2 => cur=2; loop ends, cur=2<3 => impossible, output -1. Works.
Now, one more test: n=1, k=5, d=3 -> cur check: cur=1, i=0: cur>=1 -> break, cur=1>=1 => feasible. assignment: student0: x=0; day0:0%5+1=1; x=0; day1:1; day2:1; output: 1; 1; 1; correct.
Now, n=1000, k=2, d=10 -> cur check: will become >1000 quickly, feasible. assignment: all distinct.
Now, edge: d=1000, k=1, n=1: feasible. assignment: for each day, bus 1. Works.
Now, consider n=0? Not in constraints.
Thus solution is correct.
Now, let's consider if there is any hidden constraint: "The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day." So we output integers in [1,k]. Our assignment uses values up to k, which is fine.
Now, time to implement in C++.
One potential improvement: Instead of storing entire matrix, we could output rows on the fly using the formula: For day i (0-indexed), the bus for student j is ( (j / (k^i)) % k ) + 1. But careful: if we treat day i as the i-th digit from least significant, then for day i, digit = (j / k^i) % k. That's a direct formula. However, k^i may overflow for large i. But we only need to compute for j up to n-1 (<=999), so we can compute power_i = min(k^i, something). Actually (j / k^i) % k, if k^i > j then (j / k^i) = 0, so digit = 0. So we can compute power = 1, then for i from 0 to d-1: for each j, output ((j / power) % k) + 1. That avoids storing and avoids modulo division loops per student. But we need to compute power_i, which is k^i. For i up to 999, k may be large, but power_i will quickly exceed j (since j <= 999). We can compute power as a long long and check for overflow: if power > n, then (j / power) is 0 for all j. So we can break early: for days beyond a certain point, all bus numbers become 1 (since digit 0+1). So we can output rows accordingly. But it's simpler to just store.
Given constraints, storing is fine.
But we must be careful: d can be up to 1000, n up to 1000, so matrix size 1e6 ints; that's fine.
Implementation details:
-
Use fast I/O? Not necessary for 1e6 numbers.
-
Output: For each day, output numbers separated by spaces, newline at end.
Now, let's produce final solution.
But before finalizing, verify with some random tests to ensure no pair of students have identical vectors.
Our assignment uses base-k representation of numbers 0..n-1, which are distinct, so vectors are distinct. So condition satisfied.
Now, think about potential off-by-one: When k=1, n must be 1, we output all ones. That's distinct because only one vector.
Now, is there any scenario where k=0? No.
Thus solution is complete.
But wait, there is another nuance: The problem says "The company has k buses and has a contract with a school which has n students." It doesn't say k >= n or anything. So if k is less than n, but d > 1, it could still be possible (e.g., sample 1: k=2, n=3, d=2). So condition is n <= k^d, not n <= k.
Thus our approach is correct.
Now, let's think about alternative interpretations: Could the definition "two students will become close friends if and only if they are in the same buses for all d days" mean that if on each day they are in the same bus (but the bus can be different each day), they become close friends. So indeed they need to share a bus on each day. So distinct vectors.
Thus solution stands.
Now, one more edge: If d is 0? Not allowed.
Now, check with n=1000, k=1, d=1000: k^d =1, n=1000>1 -> impossible -> -1.
Now, final code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
long long n, k, d;
cin >> n >> k >> d;
// check feasibility
long long cur = 1;
for (int i = 0; i < d; ++i) {
if (cur >= n) break;
if (cur > n / k) {
cur = n;
break;
}
cur *= k;
}
if (cur < n) {
cout << -1 << endl;
return 0;
}
// produce assignment
vector<vector<int>> ans(d, vector<int>(n));
for (int student = 0; student < n; ++student) {
long long x = student; // use long long to avoid overflow in modulo?
for (int day = 0; day < d; ++day) {
ans[day][student] = (x % k) + 1;
x /= k;
}
}
// output
for (int day = 0; day < d; ++day) {
for (int student = 0; student < n; ++student) {
cout << ans[day][student];
if (student != n - 1) cout << " ";
}
cout << endl;
}
return 0;
}
Test with sample:
Input:
3 2 2
Our output:
1 2 1
1 1 2
This should be accepted.
But let's double-check the formatting: Each line ends with a space? In our code we only put space if not the last student, so no trailing space. That's fine.
Now, note: In the sample output, there is a trailing space after the last number? The sample output shows "1 1 2 " with a space at end? Actually sample output:
1 1 2
1 2 1
There is a space after last number and before newline. But it's not required. Usually Codeforces ignores trailing spaces. So fine.
Now, consider large k: k up to 1e9, we use long long for k. In ans[day][student] we assign (x % k) + 1; x is long long, k is long long, result fits in int (since < 1e9+1). So it's fine.
Now, consider d up to 1000, n up to 1000, so ans is 1e6 ints, memory ~4 MB, fine.
Thus solution is complete.
Now, think about potential optimization: none needed.
Edge: n=0 not allowed.
Now, we should also consider if k=0? Not allowed.
All good.
Now, double-check feasibility: If d=0? Not allowed.
Now, we can also think about using most significant digit first to mimic sample, but not needed.
Now, I'll produce final answer with explanation.
But let's also examine if there is any trap: The condition "no two students become close friends if they are in the same buses for all d days" - if they are in same bus on some days but different on others, they are not close friends. So yes.
Now, consider n=1, any k,d: answer is d lines each "1". Works.
Now, consider n=2, k=1, d=1: k^d=1 < 2, impossible -> -1. Good.
Now, test with n=2, k=2, d=1: possible, we output one line: "1 2"? Our assignment: student0:1, student1:2 -> "1 2". That's valid.
Now, test with n=2, k=2, d=2: k^d=4>=2, possible. Output:
day1: 1 2
day2: 1 1? Wait: student0: (0%2+1=1, 0//2=0 => 1) -> (1,1). student1: (1%2+1=2, 0=>1) -> (2,1). So day1: 1 2, day2: 1 1. So student1 vector (1,1), student2 vector (2,1). Distinct? Yes. But note student1 and student2 are not close friends because on day1 they differ? Actually student1 bus 1, student2 bus 2 on day1, so they are not same every day. So fine.
Now, what about if k=2, d=2, n=4 (max). Our assignment yields vectors: 0:(1,1),1:(2,1),2:(1,2),3:(2,2). All distinct, fine.
Now, check if any bus number exceeds k? For n=4, k=2, student3 gets (2,2) both digits 2, so bus numbers 2, which is within 1..2.
All good.
Now, one might think about using base-k representation with digits from 1..k instead of 0..k-1 then +1. We used +1, correct.
Now, confirm that our feasibility condition uses n <= k^d. For n=0? no.
Thus final solution.
Now, let's reflect on whether there is any case where the assignment fails due to bus numbers being out of range because we use modulo but if k is very large, we get values up to k, but k is allowed. So fine.
Now, finalize.
But let's also think about an alternative: If d is very large (1000) and n is 1000, k is 1e9, our assignment will have day1: values 1..1000, days 2..1000: all 1. That's fine. However, check distinctness: Student i and j differ on day1 because they have different bus numbers (i+1 vs j+1). So distinct.
Now, test boundary: n=1000, k=1000, d=1: day1: 1..1000, fine.
Now, consider n=1000, k=31, d=2: k^2=961 < 1000 => impossible -> -1. Actually 31^2=961, so indeed impossible. So condition correctly fails.
Now, the algorithm is correct.
Now, we can also think about using direct mapping: assign each student a unique code of base k with d digits. If d is large and k small, we can represent many numbers. Good.
Thus finalize.
One more nuance: In feasibility check, we used long long cur and break if cur > n/k. But n/k may be zero if k > n? That's fine. For example, n=5, k=10, d=2: cur=1, i=0: cur<5, cur > n/k? n/k=0, 1 > 0 true, so set cur=n (5) and break. Then cur>=n, feasible. Indeed, 10^2=100>=5, so feasible. Good.
If k=1, n/k = n, and cur=1, cur > n? false, so proceed, cur stays 1, after d loops, cur remains