In this HackerRank C++ Variadics problem in c++ You are given four integers: N,S,P,Q. You will use them in order to create the sequence with the give pseudo-code. we need to calculate the number of distinct integers in the sequence.

HackerRank Bit Array in C++ problem solution

 HackerRank Bit Array in C++ problem solution

#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#ifndef ONLINE_JUDGE
#define Eo(x) { cerr << #x << " = " << (x) << endl; }
#else
#define Eo(x)
#endif

typedef long long int64;
const int inf = 0x3f3f3f3f;
const int64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
typedef double real;
const real eps = 1e-6;
typedef pair<int,int> pip;

template <typename A,typename B>
ostream& operator<<(ostream& os, const pair<A,B>& p) {
	return os << p.first << "," << p.second;
}

template <typename A>
ostream& operator<<(ostream& os, const vector<A>& v) {
	for (int i = 0; i < v.size(); i++) {
		os << v[i] << ';';
	}
	return os;
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const map<A,B>& m) {
  for (typename map<A,B>::const_iterator it = m.begin(); it != m.end(); ++it) {
    os << it->first << ":" << it->second << ";";
  }
  return os;
}

int64 gcd(int64 a, int64 b) {
	return a ? gcd(b%a,a) : b;
}

const int64 MOD = (1LL)<<(31LL);
const int64 mask = MOD-1;

int main() {
  clock_t start = clock();
  int64 n,s,p,q; cin >> n >> s >> p >> q;
  const int T = 1LL<<20LL;
  int _ = 0;
  map<int64,int> used;
  int64 cur = s;
  for (_ = 0; _ < n; _++) {
    if (!(_&0xff)) {
      if (clock()-start > CLOCKS_PER_SEC * 1.8) {
        cout << n << endl;
        return 0;
      }
    }
    auto it = used.lower_bound(cur);
    if (it != used.end() && it->first == cur) {
      cout << used.size() << endl;
      return 0;
    }
    used.insert(it, make_pair(cur,_));
    cur = (cur*p + q) & mask;
  }
  cout << n << endl;
	return 0;
}
#if 0
a[0] = S (modulo 2^31)
  for i = 1 to N-1
      a[i] = a[i-1]*P+Q (modulo 2^31) 
#endif