Problem p03268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << "->";                                        \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
template <class T> void mkuni(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class T> void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T> void print(const vector<T> &v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 7;

int n, k;
int main() {
  cin >> n >> k;
  bool fl = 0;
  // k|(a+b), k|(a+c), k|(b+c)  a>b>c
  // k|2*(a+b+C)
  // k|(a+b+c)    (k/2)|(a+b+c)
  ll ans = 0, res = 0;
  if (k == 2 || k == 1) {
    if (k == 1) {
      cout << 1ll * n * n * n;
    } else if (k == 2) {
      int a = n / 2;
      int b = n - a;
      cout << 1ll * a * a * a + 1ll * b * b * b;
    }
  } else {
    if (k % 2 == 0)
      k /= 2, fl = 1;
    for (int i = k; i <= n; i += k)
      for (int j = k; j <= n; j += k)
        for (int p = k; p <= n; p += k) {
          if ((i + j) % (2 * k) == 0 && (i + p) % (k * 2) == 0 &&
              (j + p) % (k * 2) == 0)
            ++res;
          // dbg(i,j,p);
          ans++;
        }
    if (fl)
      cout << res << endl;
    else
      cout << ans << endl;
  }

  return 0;
}
// 6 5 4 3 2 1
// 5 3 1 -1 -)

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << "->";                                        \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
template <class T> void mkuni(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class T> void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T> void print(const vector<T> &v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 7;

int n, k;
int main() {
  cin >> n >> k;
  bool fl = 0;
  // k|(a+b), k|(a+c), k|(b+c)  a>b>c
  // k|2*(a+b+C)
  // k|(a+b+c)    (k/2)|(a+b+c)
  ll ans = 0, res = 0;
  if (k == 2 || k == 1) {
    if (k == 1) {
      cout << 1ll * n * n * n;
    } else if (k == 2) {
      int a = n / 2;
      int b = n - a;
      cout << 1ll * a * a * a + 1ll * b * b * b;
    }
  } else {
    if (k & 1) {
      cout << 1ll * (n / k) * (n / k) * (n / k);
    } else {
      // 54872+
      int kk = k / 2;
      kk = n / kk;
      int a = kk / 2;
      int b = kk - a;
      cout << 1ll * a * a * a + 1ll * b * b * b;
    }
  }

  return 0;
}
// 6 5 4 3 2 1
// 5 3 1 -1 -)