#include <iostream>
#include <climits>
using namespace std;
int min_squares(int m, int n) {
if (m == n)
return 1;
static int cache[100][100]; // Automatically initialized to 0 since static
if (m < n)
swap(m, n);
if (cache[m][n])
return cache[m][n];
int x = INT_MAX;
for (int i = 1; i+i <= n; ++i)
x = min(x, min_squares(m, i) + min_squares(m, n-i));
for (int i = 1; i+i <= m; ++i)
x = min(x, min_squares(i, n) + min_squares(m-i, n));
return cache[m][n] = x;
}
int main() {
int m = 6;
int n = 5;
cout << min_squares(m, n) << endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y2xpbWl0cz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgppbnQgbWluX3NxdWFyZXMoaW50IG0sIGludCBuKSB7CiAgICBpZiAobSA9PSBuKQogICAgICAgIHJldHVybiAxOwoKICAgIHN0YXRpYyBpbnQgY2FjaGVbMTAwXVsxMDBdOyAvLyBBdXRvbWF0aWNhbGx5IGluaXRpYWxpemVkIHRvIDAgc2luY2Ugc3RhdGljCgogICAgaWYgKG0gPCBuKQogICAgICAgIHN3YXAobSwgbik7CiAgICBpZiAoY2FjaGVbbV1bbl0pCiAgICAgICAgcmV0dXJuIGNhY2hlW21dW25dOwoKICAgIGludCB4ID0gSU5UX01BWDsKCiAgICBmb3IgKGludCBpID0gMTsgaStpIDw9IG47ICsraSkKICAgICAgICB4ID0gbWluKHgsIG1pbl9zcXVhcmVzKG0sIGkpICsgbWluX3NxdWFyZXMobSwgbi1pKSk7CgogICAgZm9yIChpbnQgaSA9IDE7IGkraSA8PSBtOyArK2kpCiAgICAgICAgeCA9IG1pbih4LCBtaW5fc3F1YXJlcyhpLCBuKSArIG1pbl9zcXVhcmVzKG0taSwgbikpOwoKICAgIHJldHVybiBjYWNoZVttXVtuXSA9IHg7Cn0KCgppbnQgbWFpbigpIHsKICAgIGludCBtID0gNjsKICAgIGludCBuID0gNTsKCiAgICBjb3V0IDw8IG1pbl9zcXVhcmVzKG0sIG4pIDw8IGVuZGw7Cn0K