Problem:
Score : 600 points

Problem Statement

We have an integer sequence A of length N, where A_1 = X, A_{i+1} = A_i + D (1 \leq i < N ) holds.

Takahashi will take some (possibly all or none) of the elements in this sequence, and Aoki will take all of the others.

Let S and T be the sum of the numbers taken by Takahashi and Aoki, respectively. How many possible values of S - T are there?

Constraints

-10^8 \leq X, D \leq 10^8

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X D

Output

Print the number of possible values of S - T.

Sample Input 1

3 4 2

Sample Output 1

8

A is (4, 6, 8).

There are eight ways for (Takahashi, Aoki) to take the elements: ((), (4, 6, 8)), ((4), (6, 8)), ((6), (4, 8)), ((8), (4, 6))), ((4, 6), (8))), ((4, 8), (6))), ((6, 8), (4))), and ((4, 6, 8), ()).

The values of S - T in these ways are -18, -10, -6, -2, 2, 6, 10, and 18, respectively, so there are eight possible values of S - T.

Sample Input 2

2 3 -3

Sample Output 2

2

A is (3, 0). There are two possible values of S - T: -3 and 3.

Sample Input 3

100 14 20

Sample Output 3

49805

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

// X, X + D, X + 2D, X + 3D

// 長さ 3 = (3X + 3 * 2 / 2 * D) + 3D * i
// 長さ L = (LX + L * (L-1) / 2 * D) + L * D * i

// S - T, S + T = N
// N - 2T

vector<pair<ll,pair<ll,ll>>> vec;

int main() {
  ll N, X, D;

  cin >> N >> X >> D;

  if (D < 0) {
    D = -D;
    X = -X;
  }

  int l = 0, r = 0;
  for (ll i=0; i<=N; i++) {
    ll B = X * i;
    ll P = B % D;
    ll Q = B / D;
    if (P < 0) {
      P += D;
      Q -= 1;
    }

    if (i > 0) {
      l += i-1;
      r += N-i;
    }

    vec.push_back({P, {Q+l, Q+r+1}});
  }

  sort(vec.begin(), vec.end());

  ll ans = 0, prev = -1;
  l = 0, r = 0;

  for (int i=0; i<=N; i++) {

    debug(vec[i]);
    debug(r); debug(l);

    if (prev != vec[i].first || r < vec[i].second.first) {
      ans += r - l;
      prev = -1;
      r = 0; l = 0;
    }

    if (prev != vec[i].first) {
      l = vec[i].second.first;
      r = vec[i].second.second;
    } else {
      r = vec[i].second.second;
    }

    prev = vec[i].first;
  }

  ans += r - l;


  cout << ans << endl;


  return 0;
}