Problem p03146 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define ll long long
using namespace std;
static ll imin = std::numeric_limits<ll>::min();
static ll imax = std::numeric_limits<ll>::max();
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define rev_rep(i, n) for (int i = n - 1; i >= 0; --i)
#define dump(x) cout << #x << ":" << x << "\n";
#define all(v) v.begin(), v.end()

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::map<T1, T2> &mp);
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> &pair);
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &vec);
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> &pair) {
  os << '(' << pair.first << ',' << pair.second << ')';
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &vec) {
  std::size_t n = vec.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (int i = 0; i < n - 1; ++i) {
    os << vec[i] << " ";
  }
  os << vec[n - 1];
  os << std::endl;
  return os;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::map<T1, T2> &mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto &&x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> &mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto &&x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

ll f(ll x) {
  if (x % 2 == 0) {
    return x / 2;
  } else {
    return x * 3 + 1;
  }
}

#ifndef BOOST_TEST_MAIN
int main(int const argc, char const **argv) {

  ll s;
  cin >> s;

  set<ll> memory;
  int res = 0;
  memory.insert(s);
  while (memory.find(s) == memory.end() || res == 0) {
    // dump(memory);
    // dump(s);
    // memory.insert(s);
    s = f(s);
    res++;
  };
  cout << res + 1;
}
#endif


Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define ll long long
using namespace std;
static ll imin = std::numeric_limits<ll>::min();
static ll imax = std::numeric_limits<ll>::max();
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define rev_rep(i, n) for (int i = n - 1; i >= 0; --i)
#define dump(x) cout << #x << ":" << x << "\n";
#define all(v) v.begin(), v.end()

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::map<T1, T2> &mp);
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> &pair);
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &vec);
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> &pair) {
  os << '(' << pair.first << ',' << pair.second << ')';
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &vec) {
  std::size_t n = vec.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (int i = 0; i < n - 1; ++i) {
    os << vec[i] << " ";
  }
  os << vec[n - 1];
  os << std::endl;
  return os;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::map<T1, T2> &mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto &&x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> &mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto &&x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

ll f(ll x) {
  if (x % 2 == 0) {
    return x / 2;
  } else {
    return x * 3 + 1;
  }
}

#ifndef BOOST_TEST_MAIN
int main(int const argc, char const **argv) {

  ll s;
  cin >> s;

  set<ll> memory;
  int res = 0;
  memory.insert(s);
  while (memory.find(s) == memory.end() || res == 0) {
    // dump(memory);
    // dump(s);
    memory.insert(s);
    s = f(s);
    res++;
  };
  cout << res + 1;
}
#endif