Sort by

recency

|

306 Discussions

|

  • + 0 comments

    So essentially we're dealing with a finite integer field of 2^31. So the key is to first test whether S, Q or P are coprime with 2^31. If any of them are coprime that means the sequence will NOT repeat. Thus the result will be N.

    Only go on after that check with the obvious algorithm of calculating the sequence and seeing when the sequence repeats. And you can stop as soon as you encounter a previously appearing number.

  • + 0 comments

    Disclamer: a bit upset with the problem statement this pseudocode tells us that the sequence is actually linear(arithmetical) and (mathematecaly) unbounded from the top. so there is no use of Q%(2<<30) except once so the sequence is a = p*a+q there is no chance to repeate the value except overflow but we do not know what data type we should use for the sequence and it is machine specific. as the result: the solution

    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        unsigned int N,S,P,Q;
        cin>>N>>S>>P>>Q;
        Q %=(2<<30);
        
        unsigned int a0,a;
        a0 = S%(2<<30);
        
        for(unsigned int i=0; i<N-1;i++){
            a = a0*P+Q;
            if (a0==a){
                N= i;
                break;}
            a0=a;
        }
    
    cout<<N;
    

    } debuged on test case 8 worked for my local PC for unsigned long but for HackerRank envirement only for unsigned int

  • + 0 comments

    my code

    include

    include

    using namespace std;

    int main() { long long N, S, P, Q; cin >> N >> S >> P >> Q;

    set<long long> uniqueNumbers; 
    long long current = S;
    
    for (long long i = 0; i < N; ++i) {
        uniqueNumbers.insert(current);
        current = (current * P + Q) % (1LL << 31);
    }
    
    cout << uniqueNumbers.size() << endl; // Output the number of distinct integers
    return 0;
    

    }

  • + 0 comments

    the pseudo code is wrong:

    a[0] = S (modulo 2^31) for i = 1 to N-1 a[i] = a[i-1]*P+Q (modulo 2^31)

    The loop must be in C++: for (i = 1; i <= N; ++i)

  • + 0 comments

    I tested the output for the following input using a std::set 100000000 1232077670 126810854 1536183938 The website claims 26 but I'm getting 27. Giving up!