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>
using namespace std;

int main(){
    int64_t n, x, d;
    cin >> n >> x >> d;

    if(d == 0){
        cout << n+1 << endl;
        return 0;
    }

    map<int64_t, vector<pair<int64_t, int64_t>>> mp;

    for(int k=0;k<=n;++k){
        int64_t rem = k*x % d;
        while(rem < 0) rem += abs(d);

        if(mp.count(rem) == 0) mp[rem] = {};
        int64_t offset = (k * x - rem) / d;
        mp[rem].emplace_back(offset + k*(k-1)/2, offset + n*(n-1)/2 - (n-k-1)*(n-k)/2);
        cerr << k*x / d + k*(k-1)/2 << " " << k*x / d + n*(n-1)/2 - (n-k-1)*(n-k)/2 << endl;
    }

    int64_t ans = 0;
    for(auto p: mp){
        map<int64_t, int64_t> coor2idx;
        vector<int64_t> idx2coor;

        set<int64_t> coor;
        for(auto c: p.second){
            coor.insert(c.first);
            coor.insert(c.second);
            coor.insert(c.second+1);
        }
        int i = 0;
        for(auto c: coor){
            coor2idx[c] = i;
            idx2coor.push_back(c);
            ++i;
        }
        vector<int64_t> sum(i);
        for(auto c: p.second){
            ++sum[coor2idx[c.first]];
            --sum[coor2idx[c.second+1]];
        }
        for(int j=1;j<i;++j) sum[j] += sum[j-1];

        bool stat = true;
        int64_t beg = idx2coor[0];
        for(int j=1;j<i;++j){
            if(sum[j] <= 0 && stat){
                ans += idx2coor[j]-beg;
                stat = false;
            }
            if(sum[j] > 0 && !stat){
                stat = true;
                beg = idx2coor[j];
            }
        }
    }

    cout << ans << endl;
    
    return 0;
}