HackerRank C++ Bit Array




Your task is to calculate the number of distinct integers in the sequence a.



  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. #define f(x, p, q) ( (x * p) + q )
  8. typedef unsigned long long ULONG;
  9. int main() {
  10. int n, s, p, q;
  11. cin >> n >> s >> p >> q;
  12. ULONG p_231 = static_cast<ULONG>(pow(2, 31));
  13. ULONG x0 = s % p_231;
  14. ULONG vals = 1;
  15. ULONG tort = f(x0, p, q) % p_231;
  16. ULONG hare = f(x0, p, q) % p_231;
  17. hare = f(hare, p, q) % p_231;
  18. while (tort != hare) {
  19. if (vals >= n) {
  20. cout << vals << endl;
  21. return 0;
  22. } else {
  23. vals++;
  24. }
  25. tort = f(tort, p, q) % p_231;
  26. hare = f(hare, p, q) % p_231;
  27. hare = f(hare, p, q) % p_231;
  28. }
  29. ULONG mu = 0;
  30. tort = x0;
  31. while (tort != hare) {
  32. tort = f(tort, p, q) % p_231;
  33. hare = f(hare, p, q) % p_231;
  34. mu++;
  35. }
  36. ULONG lam = 1;
  37. hare = f(tort, p, q) % p_231;
  38. while (tort != hare) {
  39. hare = f(hare, p, q) % p_231;
  40. lam++;
  41. }
  42. cout << lam + mu << endl;
  43. return 0;
  44. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube