• + 5 comments

    I don't understand all the complicated solutions here. There is someone talking about overflow being well defined in C++ for unsigned types, and there happens to be a common 32bit unsigned integer type usually available in unsigned int. So my simple solution which passes all tests seems best.

    #include <iostream>
    
    
    int main() {
        static_assert(sizeof(unsigned int) == 4);
        unsigned int N, S, P, Q;
        
        std::cin >> N >> S >> P >> Q;
        
        auto count =0;
        auto previous = S;
        while (N--) {
            auto brandnew = previous * P + Q;
            if (brandnew != previous) ++count;
            previous = brandnew;
        }
           
        std::cout << count;
        return 0;
    }