Untitled
unknown
c_cpp
a month ago
1.8 kB
1
Indexable
Never
#include<bits/stdc++.h> using namespace std; using ll = long long; void File() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); #ifndef ONLINE_JUDGE freopen("errors.txt", "w", stderr); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } vector<long long>divisors(long long n) { vector<long long> div; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { div.push_back(i); if (n / i != i) div.push_back(n / i); } } return div; } const int N = 1e5 + 5; vector<vector<int>> vis, a; vector<pair<int, int>> id[N]; int n, m; bool valid(int i, int j, int d) { return i >= 0 && i < n && j >= 0 && j < m && vis[i][j] != d && a[i][j] % d == 0; } int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int dfs(int i, int j, int d) { vis[i][j] = d; int cnt = 1; for (int k = 0; k < 4; ++k) { int nx = dx[k] + i, ny = dy[k] + j; if (valid(nx, ny, d)) cnt += dfs(nx, ny, d); } return cnt; } int main() { File(); cin >> n >> m; vis.assign(n, vector<int>(m)); a.assign(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; for (auto d: divisors(a[i][j])) id[d].emplace_back(i, j); } } vector<int> ans(n * m + 1, 1); for (int d = 2; d < N; ++d) { int mx = 0; for (auto &[i, j]: id[d]) if (valid(i, j, d)) mx = max(mx, dfs(i, j, d)); ans[mx] = max(ans[mx], d); } for (int i = n * m - 1; i > 0; --i) ans[i] = max(ans[i], ans[i + 1]); for (int i = 1; i <= n * m; ++i) cout << ans[i] << " "; return 0; }
Leave a Comment