• + 0 comments
    #include <iostream>
    using namespace std;
    
    const unsigned long long MOD = 1ULL << 31;
    const int BITSET_SIZE = MOD / 64; // Number of 64-bit blocks (approx 33.5 million)
    
    unsigned long long mask[BITSET_SIZE] = {0};
    
    bool insert(unsigned x) {
        unsigned idx = x >> 6;    // Divide by 64 to get block index
        unsigned pos = x & 63;    // Modulo 64 to get bit position
        if ((mask[idx] & (1ULL << pos)) == 0) {
            mask[idx] |= (1ULL << pos);
            return true;
        }
        return false;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        unsigned long long N, S, P, Q;
        cin >> N >> S >> P >> Q;
    
        unsigned long long a = S % MOD;
        unsigned long long distinct = 0;
    
        // Insert first element
        if (insert(a)) distinct++;
    
        for (unsigned long long i = 1; i < N; i++) {
            a = (a * P + Q) % MOD;
            if(insert(a))
                distinct++;
        }
    
        cout << distinct << "\n";
        return 0;
    }