#include <iostream>
#include <vector>
#include <stdint.h>
#include <limits>
class Collatz
{
private:
typedef unsigned short elem_t;
typedef std::vector<elem_t> vector_t;
static const elem_t MAX = 65535;
uint64_t s;
vector_t v;
public:
Collatz(vector_t::size_type s) : s(s), v(s + 1, MAX) { v[1] = 0; }
uint64_t get_at_f(uint64_t i)
{
bool inrange = i <= s;
if (inrange && (v[i] != MAX))
{
return v[i];
}
else
{
auto result = get_at_f( ((i % 2 == 0) ? i : 3 * i + 1) / 2 ) + 1;
if (inrange)
{
v[i] = result;
}
return result;
}
}
};
int main()
{
const std::size_t size = 1000000;
Collatz x(size);
uint64_t cur_max = 0;
uint64_t cur_max_i = 0;
for (uint64_t i = 1; i <= size; ++i)
{
auto result = x.get_at_f(i);
if (result >= cur_max)
{
cur_max = result;
cur_max_i = i;
}
}
std::cout << cur_max_i << ' ' << cur_max << std::endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RkaW50Lmg+CiNpbmNsdWRlIDxsaW1pdHM+CgpjbGFzcyBDb2xsYXR6CnsKICBwcml2YXRlOgogICAgdHlwZWRlZiB1bnNpZ25lZCBzaG9ydCBlbGVtX3Q7CiAgICB0eXBlZGVmIHN0ZDo6dmVjdG9yPGVsZW1fdD4gdmVjdG9yX3Q7CiAgICBzdGF0aWMgY29uc3QgZWxlbV90IE1BWCA9IDY1NTM1OwogICAgdWludDY0X3QgczsKICAgIHZlY3Rvcl90IHY7CiAgcHVibGljOgogICAgQ29sbGF0eih2ZWN0b3JfdDo6c2l6ZV90eXBlIHMpIDogcyhzKSwgdihzICsgMSwgTUFYKSB7IHZbMV0gPSAwOyB9CiAgICB1aW50NjRfdCBnZXRfYXRfZih1aW50NjRfdCBpKQogICAgewogICAgICBib29sIGlucmFuZ2UgPSBpIDw9IHM7CiAgICAgIGlmIChpbnJhbmdlICYmICh2W2ldICE9IE1BWCkpCiAgICAgIHsKICAgICAgICByZXR1cm4gdltpXTsKICAgICAgfQogICAgICBlbHNlCiAgICAgIHsKICAgICAgICBhdXRvIHJlc3VsdCA9IGdldF9hdF9mKCAoKGkgJSAyID09IDApID8gaSA6IDMgKiBpICsgMSkgLyAyICkgKyAxOwogICAgICAgIGlmIChpbnJhbmdlKQogICAgICAgIHsKICAgICAgICAgIHZbaV0gPSByZXN1bHQ7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgIH0KICAgICB9Cn07CgppbnQgbWFpbigpCnsKICBjb25zdCBzdGQ6OnNpemVfdCBzaXplID0gMTAwMDAwMDsKICBDb2xsYXR6IHgoc2l6ZSk7CiAgdWludDY0X3QgY3VyX21heCA9IDA7CiAgdWludDY0X3QgY3VyX21heF9pID0gMDsKICBmb3IgKHVpbnQ2NF90IGkgPSAxOyBpIDw9IHNpemU7ICsraSkKICB7CiAgICBhdXRvIHJlc3VsdCA9IHguZ2V0X2F0X2YoaSk7CiAgICBpZiAocmVzdWx0ID49IGN1cl9tYXgpCiAgICB7CiAgICAgIGN1cl9tYXggPSByZXN1bHQ7CiAgICAgIGN1cl9tYXhfaSA9IGk7CiAgICB9CiAgfQogIHN0ZDo6Y291dCA8PCBjdXJfbWF4X2kgPDwgJyAnIDw8IGN1cl9tYXggPDwgc3RkOjplbmRsOwp9