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;

#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for(int x = y; x <= z; x++)
#define REPP(x, y, z) for(int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x)) 
#define EB emplace_back
#define maxn 

//structure

//declaration
//functions

int main(void)
{
	#ifdef DBG
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
	#endif

	int n, x, d;
	set<int> div;
	int total = 0;

	scanf("%d %d %d", &n, &x, &d);

	if(d == 0)
	{
		if(x == 0)
			printf("1\n");
		else
			printf("%d\n", 1+n);

		return 0;
	}

	if(d < 0)
	{
		d *= -1;
		x *= -1;
	}

	unordered_map<int, vector<pair<ll, ll> > > mp;

	REP(k, 0, n)
	{
		ll modd = (k*x)%d >= 0 ? (k*x)%d : (-(k*x)%d)+d;
		ll kxmod=( x<0 ? (k*x)/d -1 : (k*x)/d );

		mp[modd].PB(MP(kxmod+(k-1)*k/2, kxmod+(2*n-k-1)*k/2));
	}

	ll ans = 0;

	for(auto i : mp)
	{
		auto j = i.S;

		sort(all(j));

		ll left = -1e9, right = -1e9;

		for(auto x : j)
		{
			ll le = x.F;
			ll ri = x.S;

			if(left == -1e9)
			{
				left = le; right = ri;
				continue;
			}

			if(le > right)
			{
				ans += right-left+1;
				left = le; right = ri;
				continue;
			}

			if(ri > right)
			{
				right = ri;
			}
		}

		ans += right-left+1;

	}

	printf("%lld\n", ans);

	return 0;
}

// #include <bits/stdc++.h>
// #define SORT(x) sort((x).begin(), (x).end())
// #define ALL(x) (x).begin(), (x).end()
// #define rep(i, n) for (ll i = 0; i < n; i++)
// #define reps(i, m, n) for (ll i = m; i < n; i++)
// #define repr(i, m, n) for (ll i = m; i >= n; i--)
// #define de(x) cout << #x << "=" << x << endl;
// template <class T>
// bool maxi(T &a, const T &b) {
//     if (a < b) {
//         a = b;
//         return 1;
//     }
//     return 0;
// }
// template <class T>
// bool mini(T &a, const T &b) {
//     if (b < a) {
//         a = b;
//         return 1;
//     }
//     return 0;
// }
// #define INF2 1000000000000000037
// #define INF  1000000007
// #define MOD  1000000007
// using namespace std;
// using ll = long long;
// using P  = pair<ll,ll>;
// //--GLOBAL---------------------------------
 
// //--MAIN-----------------------------------
// void Main() {
//     ll N,X,D; cin>>N>>X>>D;
//     if(D==0){
//         if(X==0) cout<< 1 <<"\n";
//         else cout<< N+1 <<"\n";
//         return;
//     }
//     if(D<0){
//         D=-D;
//         X=-X;
//     }
//     unordered_map<ll,vector<P>> mp;
//     rep(k,N+1){
//         ll kxmodd=( (k*X)%D>=0 ? (k*X)%D : (-(k*X)%D)+D );
//         ll kxmodsho=( X<0 ? (k*X)/D -1 : (k*X)/D );
//         ll left=kxmodsho+(k*(k-1))/2;
//         ll right=kxmodsho+((2*N-k-1)*k)/2;
//         mp[kxmodd].emplace_back(left,right);
//     }
//     ll ans=0;
//     for(auto& x: mp){
//         auto& pev=x.second;
//         SORT(pev);

//         for(auto i : pev)
//         	cout << i.first << ' ' << i.second << endl;
//         puts("");
//         ll right=-INF2;
//         for(auto& pe: pev){
//             ll le=pe.first;
//             ll ri=pe.second;
//             if(ri<=right) continue;
//             if(le>right){
//                 ans+=ri-le+1;
//             }
//             else{
//                 ans+=ri-right;
//             }
//             right=ri;
//         }
//     }
//     cout<< ans <<"\n";
// }
 
// //--START----------------------------------
// int main() {
//     cin.tie(nullptr);
//     ios_base::sync_with_stdio(false);
//     cout << fixed << setprecision(15);
//     Main();
// }