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: Wrong Answer)

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n;
  cin >> n;
  ll x, d;
  cin >> x >> d;
  if (x == 0LL && d == 0LL) {
    cout << 0 << endl;
    return 0;
  }
  if (d == 0LL) {
    cout << n + 1 << endl;
    return 0;
  }

  if (d < 0LL) {
    x = -x;
    d = -d;
  }

  map<ll, vector<pair<ll, ll>>> m;
  REP(i, n + 1) {
    ll key = i * x < 0 ? (d - abs(i * x) % d) % d : (i * x) % d;
    ll mi = i == 0 ? i * x / d : i * x / d + i * (i - 1LL) / 2LL;
    ll ma = i * x / d + (i * n - (i * (i + 1LL) / 2LL));
    m[key].push_back(make_pair(mi, ma));
  }
  ll ans = 0;
  for (auto keyval : m) {
    auto val = keyval.second;
    sort(ALL(val));
    ll exma;
    REP(i, val.size()) {
      ans += (val[i].second - val[i].first) + 1LL;
      if (i != 0) {
        if (exma >= val[i].second) {
          ans -= max(0LL, (val[i].second - val[i].first) + 1LL);
        } else if (exma < val[i].second) {
          ans -= max(0LL, (exma - val[i].first) + 1LL);
        }
      }
      exma = val[i].second;
    }
  }
  cout << ans << endl;
}