개발 공부/알고리즘

HackerRank, C++, Bit Array

그냥하는티스토리 2022. 11. 10. 20:22
728x90

data type에 따른 값의 크기와,

cycle을 어떻게 처리할지에 대해 고민 할 수 있는 문제

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#include <set>

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    unsigned int N,S,P,Q;
    
    cin >> N;
    cin >> S >> P >> Q;
    
    unsigned int bigNumber = pow(2,31);
    
    unsigned int a0 = S;
    unsigned long long a = a0;
    unsigned long long a1 = a0;
    unsigned int i = 1;
    for(; i < N; i++)
    {
        a = (a0*P + Q) % bigNumber;
        if(a == S || a == a0 )
        {
            break;    
        }    
        a0 = a;    
    }
    
    
    
    cout << i << endl;
    return 0;
}
728x90