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: Accepted)

#include "bits/stdc++.h"
using namespace std;
using ll     = long long;
using pii    = pair<int, int>;
using pll    = pair<ll, ll>;
using vi     = vector<int>;
using vl     = vector<ll>;
using vvi    = vector<vi>;
using vvl    = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
    std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " "));
    os << std::endl;
}

// greatest common divisor and least common multiple
// gcd is calculated by Euclidean Algorithm
// lcm = m * n / gcd(m,n)
template <typename T = int>
T gcd(T a, T b) {
    if (a < b)
        return gcd(b, a);
    T r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

template <typename T = int>
T lcm(T m, T n) {
    if ((0 == m) || (0 == n))
        return 0;
    return ((m / gcd(m, n)) * n);
}

int main() {
    ll n, x, d;
    cin >> n >> x >> d;
    if (d == 0) {
        cout << (x == 0 ? 1 : n + 1) << "\n";
        return 0;
    }
    if (x == 0) {
        d = 1;
    } else {
        ll g = gcd(abs(x), abs(d));
        if (d < 0)
            g *= -1;
        x /= g;
        d /= g;
    }
    vector<vector<pair<ll, int>>> imos(min(n + 1, d));
    for (ll i = 0; i <= n; ++i) {
        ll l = (0 + i - 1) * (i) / 2;
        ll r = (n - 1 + n - i) * (i) / 2;
        l += x * i / d;
        r += x * i / d;
        ll idx = i % d;
        imos[idx].emplace_back(l, 1);
        imos[idx].emplace_back(r + 1, -1);
    }
    ll ret = 0;
    for (int i = 0; i < min(n + 1, d); ++i) {
        sort(imos[i].begin(), imos[i].end());
        ll prev  = -INF;
        ll depth = 0;
        for (auto &p : imos[i]) {
            if (depth > 0) {
                ret += p.first - prev;
            }
            prev = p.first;
            depth += p.second;
        }
    }
    cout << ret << "\n";
    return 0;
}