Problem p02608 (C++) — buggy submission, verdict: Runtime Error


/** In the Name of ALLAH */

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define endl '\\n'
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a, b) memset(a, b, sizeof(a))
#define sqr(a) ((a) * (a))

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fraction()                                                             \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);

int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};

inline ll gcd(ll a, ll b) { return __gcd(a, b); }
inline ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

const int mx = 1e5 + 123;
int per[mx];

int main() {
  optimize();

  for (int i = 1; i * i < mx; i++) {
    per[i * i] = i;
  }

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {

    int cnt = 0;

    for (int x = 1; x * x <= i; x++) {
      for (int y = 1; y * y <= i; y++) {
        int a = 1;
        int b = x + y;
        int c = i - (x * x) - (y * y) - (x * y);

        int d1 = (b * b) + (4 * a * c);

        if (d1 < 0)
          continue;
        else if (per[d1] == 0)
          continue;
        else if (per[d1] - b <= 0)
          continue;

        if ((per[d1] - b) % (2 * a) == 0) {
          cnt++;
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}


Error: 0

Fixed submission:


/** In the Name of ALLAH */

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define endl '\\n'
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a, b) memset(a, b, sizeof(a))
#define sqr(a) ((a) * (a))

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fraction()                                                             \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);

int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};

inline ll gcd(ll a, ll b) { return __gcd(a, b); }
inline ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

const int mx = 1e4 + 123;
int per[mx];

int main() {
  optimize();

  for (int i = 1; i * i < mx; i++) {
    per[i * i] = i;
  }

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {

    int cnt = 0;

    for (int x = 1; x * x <= i; x++) {
      for (int y = 1; y * y <= i; y++) {
        int a = 1;
        int b = x + y;
        int c = i - (x * x) - (y * y) - (x * y);

        int d1 = (b * b) + (4 * a * c);

        if (d1 < 0)
          continue;
        else if (per[d1] == 0)
          continue;
        else if (per[d1] - b <= 0)
          continue;

        if ((per[d1] - b) % (2 * a) == 0) {
          cnt++;
          //	dbg(1);
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}