To solve the problem of finding the number of ways to color an n x m matrix using k colors such that adjacent cells (sharing a side) have different colors, we can follow these key steps:
Approach
The solution involves two main components:
- Valid First Row: The number of valid ways to color the first row (each adjacent cell in the row is different).
- Valid Subsequent Rows: The number of valid ways to color any subsequent row such that it is compatible with the row above (vertical adjacency) and has no adjacent cells in the row (horizontal adjacency).
Key Insights:
-
First Row: For a row of length m, the first cell has k choices, and each subsequent cell has (k-1) choices (different from the previous cell). So, the number of valid first rows is:
( F(m) = k \times (k-1)^{m-1} ). -
Subsequent Rows: For any valid row, the number of valid next rows (compatible with the above row) can be computed using dynamic programming. Let this count be ( T(m) ). For large m, we can use matrix exponentiation to compute ( T(m) ) efficiently.
-
Total Ways: The total number of colorings is the product of the valid first row count and ( T(m) ) raised to the power of (n-1) (since each of the remaining n-1 rows has ( T(m) ) valid options).
Solution Code
Assuming the problem requires results modulo ( 10^9 +7 ) (common in programming competitions), here's the implementation:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 +7;
using Matrix = vector<vector<long long>>;
Matrix multiply(const Matrix& a, const Matrix& b) {
int n = a.size();
Matrix res(n, vector<long long>(n,0));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<n; k++) {
res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % MOD;
}
}
}
return res;
}
Matrix matrix_pow(Matrix a, long long power) {
int n = a.size();
Matrix res(n, vector<long long>(n,0));
for(int i=0; i<n; i++) res[i][i] = 1;
while(power > 0) {
if(power % 2 ==1) res = multiply(res,a);
a = multiply(a,a);
power /= 2;
}
return res;
}
long long pow_mod(long long base, long long power) {
long long res = 1;
while(power>0) {
if(power%2 ==1) res = (res * base) % MOD;
base = (base * base) % MOD;
power /= 2;
}
return res;
}
long long compute_T(long long k, long long m) {
if(m == 1) return (k-1) % MOD;
// For m >= 2, T(m) follows recurrence: T(m) = (k-2)*T(m-1) + (k-1)*T(m-2)
// The matrix for recurrence: [[k-2, k-1], [1,0]]
Matrix trans = {{k-2, k-1}, {1,0}};
Matrix mat = matrix_pow(trans, m-2);
long long T1 = (k-1) % MOD;
long long T2 = ( (k-1) + (k-2)*(k-2) ) % MOD; // T(2)
return (mat[0][0] * T2 + mat[0][1] * T1) % MOD;
}
int main() {
long long n, m, k;
cin >> n >> m >> k;
if(k == 1) {
// Only possible if n=1 and m=1
cout << (n==1 && m==1 ? 1 :0) << endl;
return 0;
}
long long F = (k * pow_mod(k-1, m-1)) % MOD;
long long T = compute_T(k, m);
long long ans = (F * pow_mod(T, n-1)) % MOD;
cout << ans << endl;
return 0;
}
Explanation
- First Row Calculation: The number of valid first rows is computed using modular exponentiation for efficiency.
- Subsequent Rows: The count ( T(m) ) uses matrix exponentiation to handle large values of m. The recurrence relation for ( T(m) ) is derived from dynamic programming states, and matrix exponentiation helps compute it in ( O(\log m) ) time.
- Total Ways: The final result is the product of the first row count and ( T(m) ) raised to the power of (n-1), modulo ( 10^9+7 ).
This approach efficiently handles large values of n and m, making it suitable for programming competition constraints.


作者声明:本文包含人工智能生成内容。