fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define F first
  4. #define S second
  5. #define pll pair<long long, long long>
  6. #define pii pair<int, int>
  7. #define mii map <int , int>
  8. #define mll map <long long ,long long>
  9. #define vi vector <int>
  10. #define vl vector <long long>
  11. #define pb push_back
  12. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  13. #define cmp greater<int>()
  14. #define RF(F) freopen(F , "r" , stdin )
  15. #define WF(F) freopen(F , "w" , stdout)
  16. using namespace std;
  17.  
  18. const ll INF = 1e9 ;
  19. const int N = 500 + 7 ;
  20. const int mod = 1e9 + 7;
  21. int mem[N][N] ;
  22.  
  23. string get_string() {
  24.  
  25. const int N = 2e5 + 7 ;
  26. char s[N] ;
  27.  
  28. scanf("%s" , s) ;
  29.  
  30. return s ;
  31. }
  32.  
  33. void init() {
  34. memset(mem , -1 , sizeof mem) ;
  35. }
  36.  
  37. int dp(int n , int m){
  38.  
  39. if(n == m)return 0 ;
  40.  
  41. if(m == 1)return n - 1 ;
  42.  
  43. if(n == 1)return m - 1 ;
  44.  
  45. if(mem[n][m] != -1)return mem[n][m] ;
  46.  
  47. int ans = INF ;
  48.  
  49. for(int i = 1 ; i <= (n / 2) ; i ++){
  50. ans = min(ans , 1 + dp(i , m) + dp(n - i , m)) ;
  51. }
  52. for(int j = 1 ; j <= (m / 2) ; j ++){
  53. ans = min(ans , 1 + dp(n , j) + dp(n , m - j)) ;
  54. }
  55.  
  56. return mem[n][m] = ans ;
  57. }
  58.  
  59. void solve(int testCase){
  60.  
  61. int n , m ;
  62. scanf("%d %d" , &n , &m) ;
  63.  
  64. printf("%d" , dp(n , m)) ;
  65. }
  66.  
  67. int main() {
  68.  
  69. int testCase = 1;
  70.  
  71. //scanf("%d", &testCase);
  72.  
  73. while (testCase--){
  74. init();
  75. solve(testCase);
  76. }
  77.  
  78. return 0;
  79. }
Runtime error #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty