#include <iostream>
#include <vector>
using namespace std;
class solution
{
public:
int cut_rope_dp(int len)
{
if (len < 2)
return 0;
if (len == 2)
return 1;
if (len == 3)
return 2;
vector<int> results(len + 1, 0);
results[0] = 0;
results[1] = 1;
results[2] = 2;
results[3] = 3;
int max = 0;
for (int i = 4; i <= len; ++i)
{
max = 0;
for (int j = 1; j <= i >> 1; ++j)
{
int result = results[j]* results[i - j];
if (result > max)
max = result;
}
results[i] = max;
}
return results[len];
}
int cut_rope_gre(int len)
{
if (len < 2)
return 0;
if (len == 2)
return 1;
if (len == 3)
return 2;
int times_of_3 = len / 3;
int times_of_2 = 0;
if (len - times_of_3 * 3 == 1)
{
times_of_3 -= 1;
times_of_2 = 2;
}
if (len - times_of_3 * 3 == 2)
{
times_of_2 = 1;
}
return int(pow(3, times_of_3)) * int(pow(2, times_of_2));
}
};
int main()
{
solution a;
cout << a.cut_rope_dp(4) << endl;
cout << a.cut_rope_dp(8) << endl;
cout << a.cut_rope_dp(10) << endl;
cout << a.cut_rope_gre(4) << endl;
cout << a.cut_rope_gre(8) << endl;
cout << a.cut_rope_gre(10) << endl;
system("pause");
}