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;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x) 
#define REP(i, n) for (int i = 0; i < n; ++i) 
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

int N, X, D;

int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> X >> D;
    if (D == 0) {
        cout << (X == 0 ? 1 : N+1) << '\n';
    } else {
        if (D < 0) X = -X, D = -D;

        vector<tuple<int,ll,ll> > inv;
        FOR(i,0,N){
            ll b = (ll)i*X;
            inv.emplace_back((b%D+D)%D,b/D+(ll)i*(i-1)/2,b/D+(ll)i*(2*N-1-i)/2);
        }
        sort(ALL(inv));
        //for (auto& x : inv) cout << get<0>(x) << " " << get<1>(x) << " " << get<2>(x) << endl;
        ll ans = 0;
        for (int i = 0, j = 0; i < SZ(inv);) {
            ll ep = -1e18;
            while (j < SZ(inv) && get<0>(inv[j]) == get<0>(inv[i])) {
                if (get<2>(inv[j]) > ep) {
                    ans += get<2>(inv[j]) - max(ep,get<1>(inv[j])-1);
                    ep = get<2>(inv[j]);
                }
                ++j;
            }
            i = j;
        }
        cout << ans << '\n';
    }
}