Problem p03929 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define Yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "Yes" << '\n';                                                     \
  } else {                                                                     \
    cout << "No" << '\n';                                                      \
  }
#define YES(condition)                                                         \
  if (condition) {                                                             \
    cout << "YES" << '\n';                                                     \
  } else {                                                                     \
    cout << "NO" << '\n';                                                      \
  }
#define yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "yes" << '\n';                                                     \
  } else {                                                                     \
    cout << "no" << '\n';                                                      \
  }

int main() {
  ll n, k;
  cin >> n >> k;

  ll m;
  bool ok = false;
  for (ll i = 0; i < n - 2 && !ok; i++) {
    for (m = i * 7; m <= i * 7 + 5; m++) {
      if ((9 * m + 72) % 11 == k) {
        ok = true;
        break;
      }
    }
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }

  ll res = ((n - 2) * 7 - 2 - m) / 11 + 1;
  vector<bool> ok2(7);
  REP(i, 7) { ok2[i] = (49 + m - i * 3) % 7 != 0 && (49 + m - i * 3) % 7 != 6; }
  ll ans = res;
  REP(i, res) {
    if (!ok2[i % 7]) {
      ans--;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define Yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "Yes" << '\n';                                                     \
  } else {                                                                     \
    cout << "No" << '\n';                                                      \
  }
#define YES(condition)                                                         \
  if (condition) {                                                             \
    cout << "YES" << '\n';                                                     \
  } else {                                                                     \
    cout << "NO" << '\n';                                                      \
  }
#define yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "yes" << '\n';                                                     \
  } else {                                                                     \
    cout << "no" << '\n';                                                      \
  }

int main() {
  ll n, k;
  cin >> n >> k;

  ll m;
  bool ok = false;
  for (ll i = 0; i < n - 2 && !ok; i++) {
    for (m = i * 7; m <= i * 7 + 5; m++) {
      if ((9 * m + 72) % 11 == k) {
        ok = true;
        break;
      }
    }
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }

  ll res = ((n - 2) * 7 - 2 - m) / 11 + 1;
  vector<bool> ok2(7);
  REP(i, 7) { ok2[i] = (49 + m - i * 3) % 7 != 0 && (49 + m - i * 3) % 7 != 6; }
  vector<ll> c(7);
  REP(i, 7) { c[i] = res / 7; }
  REP(i, res % 7) { c[i]++; }
  ll ans = 0;
  REP(i, 7) {
    if (ok2[i])
      ans += c[i];
  }
  cout << ans << endl;
}