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)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, mut x, mut d) = (v[0], v[1], v[2]);
    if d == 0 {
        if x == 0 {
            println!("1");
            return;
        }
        println!("{}", n + 1);
        return;
    }
    if d < 0 {
        x *= -1;
        d *= -1;
    }
    let mut spans = HashMap::new();
    spans.entry(0).or_insert(vec![(0, 0)]);
    for i in 1..n + 1 {
        let modd = (d + (x * i) % d) % d;
        let mut span = spans.entry(modd).or_insert(Vec::<(i64, i64)>::new());
        let l = i * (i - 1) / 2;
        let r = n * (n - 1) / 2 - (n - i) * (n - i - 1) / 2;
        span.push(((x * i + l * d - modd) / d, (x * i + r * d - modd) / d));
    }
    let mut ans = 0;
    for spans_each_mod in spans.values() {
        let mut events = vec![];
        for &(l, r) in spans_each_mod {
            events.push((l, 0));
            events.push((r, 1));
        }
        events.sort();
        let mut l_count = 0;
        let mut start = 0;
        for (cur, kind) in events {
            if kind == 0 {
                if l_count == 0 {
                    start = cur;
                }
                l_count += 1;
            } else {
                l_count -= 1;
                if l_count == 0 {
                    ans += cur - start + 1;
                }
            }
        }
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}