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.42.0) (judge verdict: Accepted)

use std::collections::HashMap;
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

const OFFSET: i64 = 1 << 62;
fn main() {
    let n = read::<i64>();
    let mut x = read::<i64>();
    let mut d = read::<i64>();
    if d == 0 {
        if x == 0 {
            println!("{}", 1);
        } else {
            println!("{}", n + 1);
        }
        return;
    }
    if d < 0 {
        x *= -1;
        d *= -1;
    }
    let mut opens = HashMap::<i64, Vec<(i64, i64)>>::new();
    for i in 0..n + 1 {
        let cnt = n - i;
        let dsum1 = i * (i - 1) / 2 * d;
        let dsum2 = n * (n - 1) / 2 * d - dsum1;
        let dsum3 = cnt * (cnt - 1) / 2 * d;
        let dsum4 = n * (n - 1) / 2 * d - dsum3;
        let l = OFFSET + i * x + dsum1 - (cnt * x + dsum2);
        let r = OFFSET + i * x + dsum4 - (cnt * x + dsum3);
        // println!("{} {}", l - OFFSET, r - OFFSET);
        let index = l % (2 * d);
        let entry = opens.entry(index).or_insert(vec![]);
        entry.push((l, r));
    }
    let mut ans = 0;
    // println!("{:?}", opens);
    for ranges in opens.values() {
        let mut ranges = ranges.clone();
        ranges.sort();
        let mut prev = -OFFSET;
        // println!("{:?}", ranges);
        for &(l, r) in ranges.iter() {
            ans += (std::cmp::max(prev, r + 2 * d) - std::cmp::max(prev, l)) / 2 / d;
            prev = std::cmp::max(prev, r + 2 * d);
        }
    }
    println!("{}", ans);
}