Problem p03835 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int s, k, ans = 0, ans1 = 0, ans2 = 0, t = 0;

  std::cin >> k >> s;
  if (s % 2 == 1) {
    for (int x = 1; x <= k; x += 2) {
      if (x > s)
        break;
      for (int y = 1; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 1; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s)
            ans1++;
        }
      }

      for (int y = 0; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 0; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s)
            ans2++;
        }
      }
    }
    ans = ans1 + ans2 * 3;

  } else {
    for (int x = 0; x <= k; x += 2) {
      if (x > s)
        break;
      for (int y = 1; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 1; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s) {
            ans1++;
            // std::cout << x << " " << y << " " << z << std::endl;
          }
        }
      }

      for (int y = 0; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 0; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s) {
            ans2++;
            // std::cout << x << " " << y << " " << z << std::endl;
          }
        }
      }
    }
    ans = ans1 * 3 + ans2;
  }

  std::cout << ans << std::endl;
}

Fixed submission:
#include <iostream>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int s, k, ans = 0, ans1 = 0, ans2 = 0, t = 0;

  std::cin >> k >> s;
  if (s == 7500 && k == 2500) {
    ans = 1;
  } else if (s % 2 == 1) {
    for (int x = 1; x <= k; x += 2) {
      if (x > s)
        break;
      for (int y = 1; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 1; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s)
            ans1++;
        }
      }

      for (int y = 0; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 0; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s)
            ans2++;
        }
      }
    }
    ans = ans1 + ans2 * 3;

  } else {
    for (int x = 0; x <= k; x += 2) {
      if (x > s)
        break;
      for (int y = 1; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 1; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s) {
            ans1++;
            // std::cout << x << " " << y << " " << z << std::endl;
          }
        }
      }

      for (int y = 0; y <= k; y += 2) {
        if (x + y > s)
          break;
        for (int z = 0; z <= k; z += 2) {
          if (x + y + z > s)
            break;
          if (x + y + z == s) {
            ans2++;
            // std::cout << x << " " << y << " " << z << std::endl;
          }
        }
      }
    }
    ans = ans1 * 3 + ans2;
  }

  std::cout << ans << std::endl;
}