fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. template<typename T>
  12. void minimize(T& a, const T& b) {
  13. if (b < a) a = b;
  14. }
  15.  
  16. const int N = 5e2 + 5;
  17.  
  18. int a, b;
  19. int dp[N][N]; // dp[x][y] = Số bước ít nhất để cắt hình chữ nhật kích thước x x y thành các hình vuông
  20.  
  21. int main() {
  22. ios::sync_with_stdio(false);
  23. cin.tie(nullptr);
  24. cin >> a >> b;
  25.  
  26. for (int x = 1; x <= a; x++) {
  27. for (int y = 1; y <= b; y++) {
  28. if (x == y) { // hình vuông
  29. dp[x][y] = 0;
  30. continue;
  31. }
  32. dp[x][y] = INF;
  33. // Cắt theo chiều ngang
  34. for (int i = 1; i <= x - 1; i++) {
  35. minimize(dp[x][y], 1 + dp[i][y] + dp[x - i][y]);
  36. }
  37. // Cắt theo chiều dọc
  38. for (int j = 1; j <= y - 1; j++) {
  39. minimize(dp[x][y], 1 + dp[x][j] + dp[x][y - j]);
  40. }
  41. }
  42. }
  43.  
  44. cout << dp[a][b] << '\n';
  45. }
Success #stdin #stdout 0.01s 5288KB
stdin
3 5
stdout
3