Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long int
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, n) for (ll i = 1; i <= n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define riverse(v) reverse((v).begin(), (v).end())
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const ll MOD = 1e9 + 7;
vi alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
               'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

ll func(ll m) {
  ll s = m / 10;
  ll count5 = s;
  while (s > 1) {
    count5 += s / 5;
    s = s / 5;
  }
  return count5;
}

int main() {
  ll n;
  cin >> n;
  ll m = n;
  ll count2 = 0;
  ll count5 = 0;
  ll count10 = 0;
  if (n % 2 == 0) {
    while (n % 2 == 0) {
      count2 += n / 2;
      n = n / 2;
    }
  }
  if (m % 10 == 0 || m % 10 == 2 || m % 10 == 4 || m % 10 == 6 || m % 10 == 8) {
    count5 += func(m);
  } else if (m == 1 || m == 3) {
    count5 = 0;
  } else {
    while (m % 5 != 0)
      m -= 2;
    ll k = m;
    ll l = (m - 5) / 10;
    count5 += l + 1;
    m = m / 5;
    while (m > 1) {
      count5 += m / 5;
      m = m / 5;
    }
    count5 -= func(k);
  }
  cout << min(count2, count5) << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long int
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, n) for (ll i = 1; i <= n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define riverse(v) reverse((v).begin(), (v).end())
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const ll MOD = 1e9 + 7;
vi alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
               'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

ll func(ll m) {
  ll s = m / 10;
  ll count5 = s;
  while (s > 1) {
    count5 += s / 5;
    s = s / 5;
  }
  return count5;
}

int main() {
  ll n;
  cin >> n;
  ll m = n;
  ll count2 = 0;
  ll count5 = 0;
  ll count10 = 0;
  if (n % 2 == 0 && n > 0) {
    while (n % 2 == 0) {
      count2 += n / 2;
      n = n / 2;
    }
  }
  if (m % 10 == 0 || m % 10 == 2 || m % 10 == 4 || m % 10 == 6 || m % 10 == 8) {
    count5 += func(m);
  } else if (m == 1 || m == 3) {
    count5 = 0;
  } else {
    while (m % 5 != 0)
      m -= 2;
    ll k = m;
    ll l = (m - 5) / 10;
    count5 += l + 1;
    m = m / 5;
    while (m > 1) {
      count5 += m / 5;
      m = m / 5;
    }
    count5 -= func(k);
  }
  cout << min(count2, count5) << endl;
}