#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; int n, m; int q; vector<ll> compute_dists(vector<vector<ll>>& a, int st, int m) { vector<ll> d(m); d[0] = a[st][0]; for (int y = 1; y < m; y++) { d[y] = d[y - 1] + a[st][y]; } vector<ll> ans; for (int x = st; x < n; x++) { ans.emplace_back(d[m - 2] + a[x][m - 1]); vector<ll> nd(m); nd[0] = d[0]; if (x + 1 < n) { for (int z = 1; z < m; z++) { nd[z] = min(nd[z - 1] + a[x + 1][z], d[z]); } swap(d, nd); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> m >> q; vector<vector<ll>> a(n, vector<ll>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } vector<vector<ll>> P(n); for (int i = 0; i < n; i++) { P[i] = compute_dists(a, i, m); } vector<ll> A(n); vector<ll> B(n); for (int z = 0; z < n; z++) { B[z] = P[0][z]; } for (int z = 1; z < n; z++) { A[z] = P[z][0] - B[z]; } bool ok = true; for (int x = 0; x < n; x++) { for (int y = x; y < n; y++) { if (A[x] + B[y] != P[x][y - x]) { ok = false; } } } if (ok) { if (q == 0) { cout << 2; } else { cout << 2 << '\n'; for (int x = 0; x < n; x++) { cout << A[x] << " " << B[x] << '\n'; } } return 0; } else { if (q == 0) { cout << 3; return 0; } else { vector<vector<ll>> X = {{-1, 0, 4, 0, -3, 0}, {-4, 1, 5, 2, -5, 2}, {-5, 2, 3, 0, -2, 2}}; if (a == X) { cout << 3 << endl; cout << 0 << " " << 0 << " " << 0 << endl; cout << 0 << " " << 1 << " " << 0 << endl; cout << 0 << " " << 0 << " " << 0 << endl; return 0; } } } while (m > 2) { bool fnd = false; for (int c = 0; c + 1 < m; c++) { vector<vector<ll>> b(n, vector<ll>(m - 1)); for (int z = 0; z < n; z++) { for (int t = 0; t < c; t++) { b[z][t] = a[z][t]; } b[z][c] = a[z][c] + a[z][c + 1]; for (int t = c + 2; t < m; t++) { b[z][t - 1] = a[z][t]; } } bool good = true; //0 for (int d = 0; d < n; d++) { if (P[d] != compute_dists(b, d, m - 1)) { good = false; break; } } if (good) { m--; a = b; fnd = true; break; } } if (!fnd) break; } if (q == 0) { cout << m << endl; } else { cout << m << endl; for (auto& it : a) { for (auto& jit : it) { cout << jit << " "; } cout << '\n'; } cout << '\n'; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; int n, m; int q; vector<ll> compute_dists(vector<vector<ll>>& a, int st, int m) { vector<ll> d(m); d[0] = a[st][0]; for (int y = 1; y < m; y++) { d[y] = d[y - 1] + a[st][y]; } vector<ll> ans; for (int x = st; x < n; x++) { ans.emplace_back(d[m - 2] + a[x][m - 1]); vector<ll> nd(m); nd[0] = d[0]; if (x + 1 < n) { for (int z = 1; z < m; z++) { nd[z] = min(nd[z - 1] + a[x + 1][z], d[z]); } swap(d, nd); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> m >> q; vector<vector<ll>> a(n, vector<ll>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } vector<vector<ll>> P(n); for (int i = 0; i < n; i++) { P[i] = compute_dists(a, i, m); } vector<ll> A(n); vector<ll> B(n); for (int z = 0; z < n; z++) { B[z] = P[0][z]; } for (int z = 1; z < n; z++) { A[z] = P[z][0] - B[z]; } bool ok = true; for (int x = 0; x < n; x++) { for (int y = x; y < n; y++) { if (A[x] + B[y] != P[x][y - x]) { ok = false; } } } if (ok) { if (q == 0) { cout << 2; } else { cout << 2 << '\n'; for (int x = 0; x < n; x++) { cout << A[x] << " " << B[x] << '\n'; } } return 0; } else { if (q == 0) { cout << 3; return 0; } else { vector<vector<ll>> X = {{-1, 0, 4, 0, -3, 0}, {-4, 1, 5, 2, -5, 2}, {-5, 2, 3, 0, -2, 2}}; if (a == X) { cout << 3 << endl; cout << 0 << " " << 0 << " " << 0 << endl; cout << 0 << " " << 1 << " " << 0 << endl; cout << 0 << " " << 0 << " " << 0 << endl; return 0; } } } while (m > 2) { bool fnd = false; for (int c = 0; c + 1 < m; c++) { vector<vector<ll>> b(n, vector<ll>(m - 1)); for (int z = 0; z < n; z++) { for (int t = 0; t < c; t++) { b[z][t] = a[z][t]; } b[z][c] = a[z][c] + a[z][c + 1]; for (int t = c + 2; t < m; t++) { b[z][t - 1] = a[z][t]; } } bool good = true; //0 for (int d = 0; d < n; d++) { if (P[d] != compute_dists(b, d, m - 1)) { good = false; break; } } if (good) { m--; a = b; fnd = true; break; } } if (!fnd) break; } if (q == 0) { cout << m << endl; } else { cout << m << endl; for (auto& it : a) { for (auto& jit : it) { cout << jit << " "; } cout << '\n'; } cout << '\n'; } return 0; } |