Language: C++

#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==v.size()-1?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    int n;
    ll x,d;
    cin >> n >> x >> d;
    if(d == 0) {
        cout << (x == 0 ? 1 : n+1) << endl;
        return 0;
    }
    if(d < 0) {
        x += d*(n-1);
        d = -d;
    }
    vector<vector<LP>> v(d);
    rep(i,n+1) {
        int rem = (x*i%d+d)%d;
        ll l = ll(i-1)*i/2;
        ll r = ll(n-1+n-i)*i/2;
        l += (x*i-rem)/d;
        r += (x*i-rem)/d;
        v[rem].eb(l,r);
    }
    ll ans = 0;
//    auto f = [](vector<LP> &v) {
//        vl prs;
//        prs.clear();
//        ll ret = 0;
//        for(auto p : v) {
//            prs.pb(p.first);
//            prs.pb(p.second);
//        }
//        sort(all(prs));
//        prs.erase(unique(all(prs)),prs.end());
//        int sz = prs.size#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==v.size()-1?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    int n;
    ll x,d;
    cin >> n >> x >> d;
    if(d == 0) {
        cout << (x == 0 ? 1 : n+1) << endl;
        return 0;
    }
    if(d < 0) {
        x += d*(n-1);
        d = -d;
    }
    map<int,vector<LP>> mp;
    rep(i,n+1) {
        int rem = (x*i%d+d)%d;
        ll l = ll(i-1)*i/2;
        ll r = ll(n-1+n-i)*i/2;
        l += (x*i-rem)/d;
        r += (x*i-rem)/d;
        mp[rem].eb(l, r);
    }
    ll ans = 0;
    auto f = [](vector<LP> &v) {
        vl prs;
        prs.clear();
        ll ret = 0;
        for(auto p : v) {
            prs.pb(p.first);
            prs.pb(p.second);
        }
        sort(all(prs));
        prs.erase(unique(all(prs)),prs.end());
        int sz = prs.size();
        vi cnt(2*sz,0);
        for(auto p : v) {
            int l = lower_bound(all(prs),p.first)-prs.begin();
            int r = lower_bound(all(prs),p.second)-prs.begin();
            cnt[l*2]++;
            cnt[r*2+1]--;
        }
        rep(i,2*sz-1) {
            cnt[i+1] += cnt[i];
            if(cnt[i])  {
                if(i&1) ret += prs[i/2+1]-prs[i/2]-1;
                else ret += 1;
            }
        }
        return ret;
    };
    for(auto v : mp) ans += f(v.second);
//    cout << ans << endl;
}

//        vi cnt(2*sz,0);
//        for(auto p : v) {
//            int l = lower_bound(all(prs),p.first)-prs.begin();
//            int r = lower_bound(all(prs),p.second)-prs.begin();
//            cnt[l*2]++;
//            cnt[r*2+1]--;
//        }
//        rep(i,2*sz-1) {
//            cnt[i+1] += cnt[i];
//            if(cnt[i])  {
//                if(i&1) ret += prs[i/2+1]-prs[i/2]-1;
//                else ret += 1;
//            }
//        }
//        return ret;
//    };
//    rep(i,d) {
//        if(v[i].empty()) continue;
//        ans += f(v[i]);
//    }
//    cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:88:1: error: a template declaration cannot appear at block scope
   88 | template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
      | ^~~~~~~~
a.cc:90:1: error: a template declaration cannot appear at block scope
   90 | template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
      | ^~~~~~~~
a.cc:92:18: error: a function-definition is not allowed here before '{' token
   92 | void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
      |                  ^
a.cc:93:18: error: a function-definition is not allowed here before '{' token
   93 | void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
      |                  ^
a.cc:94:1: error: a template declaration cannot appear at block scope
   94 | template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
      | ^~~~~~~~
a.cc:99:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   99 | int main() {
      |         ^~
a.cc:99:9: note: remove parentheses to default-initialize a variable
   99 | int main() {
      |         ^~
      |         --
a.cc:99:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:99:12: error: a function-definition is not allowed here before '{' token
   99 | int main() {
      |            ^