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 Rust (1.15.1) (judge verdict: Accepted)

use std::collections::{BTreeMap, BTreeSet};
use std::cmp::max;

fn readln() -> Vec<String> {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_right().split_whitespace().map(|s| s.to_owned()).collect()
}

fn main() {
    let input: Vec<i64> = readln().iter().map(|s| s.parse().unwrap()).collect();
    let n = input[0];
    let mut x = input[1];
    let mut d = input[2];
    if d < 0 {
        x = -x;
        d = -d;
    }
    let x = x;
    let d = d;

    let mut ans = 0i64;
    if d == 0 {
        let mut set = BTreeSet::new();
        for i in 0..n + 1 {
            set.insert(x * i);
        }
        ans = set.len() as i64;
    } else {
        let mut map = BTreeMap::<_, Vec<(i64, i64)>>::new();
        for i in 0..n + 1 {
            let md = (i * x % d + d) % d;
            let min = (x * i - md) / d + i * (i - 1) / 2;
            let max = (x * i - md) / d + i * (2 * n - i - 1) / 2 + 1;
            map.entry(md).or_insert_with(&Vec::new).push((min, max));
        }

        for (_, v) in &mut map {
            v.sort();
            let mut maxend: i64 = i64::min_value();
            for &(f, t) in &*v {
                let u = max(maxend, f);
                maxend = max(maxend, t);
                ans += maxend - u;
            }
        }
    }
    println!("{}", ans);
}