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)

fn main() {
	let mut s = String::new();
	std::io::stdin().read_line(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: i64 = s.next().unwrap().parse().unwrap();
	let x: i64 = s.next().unwrap().parse().unwrap();
	let d: i64 = s.next().unwrap().parse().unwrap();

	if d == 0 {
		println!("{}", if x == 0 { 1 } else { n + 1 });
		return;
	}

	let mut mp = std::collections::BTreeMap::new();
	for k in 0..n + 1 {
		let l = k * (k - 1) / 2;
		let r = k * (n - 1 + n - k) / 2;
		let rem = (k * x % d + d.abs()) % d;
		mp.entry(rem).or_insert(vec![]).push(((k * x - rem) / d + l, (k * x - rem) / d + r));
	}
	let mut ans = 0;
	for (_, vec) in mp.iter() {
		let mut ev = vec.iter().flat_map(|&(l, r)| vec![(l, 1), (r + 1, -1)]).collect::<Vec<(i64, i64)>>();
		ev.sort();
		ans += ev.into_iter().fold((0, 0, 0), |s, (p, cnt)|{
			(s.0 + if s.1 != 0 { p - s.2 } else { 0 }, s.1 + cnt, p)
		}).0;
	}
	println!("{}", ans);
}