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>

int main() {
    using namespace std;
    long N;
    long X, D;
    cin >> N >> X >> D;
    if(D < 0){
        X += D * (N - 1);
        D *= -1;
    }
    if(D == 0){
        if(X == 0)return 0 & puts("1");
        cout << N + 1 << endl;
        return 0;
    }
    if(X == 0){
        X += D;
        --N;
    }
    if(N == 0){
        return 0 & puts("1");
    }
    if(X || D){
        auto g = max(__gcd(X, D), -__gcd(X, D));
        X /= g;
        D /= g;
    }
    vector<pair<long, pair<long, long>>> p(N + 1);
    auto x = (X % D + D) % D;
    for(long i = 0; i <= N; ++i){
        auto t = (x * i) % D;
        auto k = (X * i - t) / D;
        p[i] = {t, pair<long, long>({k + i * (i - 1) / 2, k + i * (2 * N - i - 1) / 2 + 1})};
    }
    sort(p.begin(), p.end());
    vector<pair<long, pair<long, long>>> q;
    for(const auto& i : p){
        if(q.empty())q.push_back(i);
        else if(i.first != q.back().first)q.push_back(i);
        else if(q.back().second.second >= i.second.first)q.back().second.second = i.second.second;
        else q.push_back(i);
    }
    long ans{0};
    for(const auto& i : q)ans += i.second.second - i.second.first;
    cout << ans << endl;
    return 0;
}