Advertisement
matheus_monteiro

Fibonacci?

Sep 6th, 2022
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. /*
  4.  * Author: Matheus Monteiro
  5.  */
  6.  
  7. using namespace std;
  8.  
  9. // #ifdef DEBUGMONTEIRO
  10. #define _ << " , " <<
  11. #define bug(x) cout << #x << "  >>>>>>>  " << x << endl;
  12. // #else
  13. // #define bug(x)
  14. // #endif
  15.  
  16. #define int long long
  17. #define Max(a, b) (a > b ? a : b)
  18. #define Min(a, b) (a < b ? a : b)
  19. #define ii pair<int, int>
  20. #define f first
  21. #define s second
  22. #define vi vector<int>
  23. #define vii vector<ii>
  24. #define LSOne(S) ((S) & (-S))
  25. #define UNTIL(t) while (clock() < (t) * CLOCKS_PER_SEC)
  26. #define SZ(a) (int)a.size()
  27. #define fastio std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
  28.  
  29. const int MAX = 2000000; //2 * 10^5
  30. const int MOD = 998244353; //10^9 + 7
  31. const int OO = 0x3f3f3f3f3f3f3f3f;
  32. const double EPS = 1e-9;  //10^-9
  33. std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  34.  
  35. int expMod(int a, int e, int m) {
  36.     int ans = 1;
  37.     while(e) {
  38.         if(e & 1) ans = (ans * a) % m, e--;
  39.         else a = (a * a) % m, e /= 2;
  40.     }
  41.     return ans;
  42. }
  43.  
  44. int remainder(const string &s, int p) {
  45.     int ans = 0;
  46.     int pot = 1;
  47.     for(int i = s.size() - 1; i >= 0; --i) {
  48.         ans = (ans + ((s[i] - '0') * pot) % p) % p;
  49.         pot = (pot * 10) % p;
  50.     }
  51.     return ans;
  52. }
  53.  
  54. bool isSquareResidue(int x, int p) {
  55.     if(x == 0) return true;
  56.     return expMod(x, (p - 1) / 2, p) == 1;
  57. }
  58.  
  59. bool isPerfectSquare(const string &s, int p) {
  60.     int r = remainder(s, p);
  61.     if(isSquareResidue((5 * r * r + 4) % p, p))
  62.         return true;
  63.     if(isSquareResidue((5 * r * r - 4) % p, p))
  64.         return true;
  65.     return false;
  66. }
  67.  
  68. bool isPrime(int x) {
  69.     if(x < 2) return false;
  70.     if(x == 2) return true;
  71.     if(x % 2 == 0) return false;
  72.     for(int i = 2; i * i <= x; ++i) {
  73.         if(x % i == 0) {
  74.             return false;
  75.         }
  76.     }
  77.     return true;
  78. }
  79.  
  80. void solve() {
  81.     string number;
  82.     cin >> number;
  83.  
  84.     bool isFib = true;
  85.     int numberOfTries = 30;
  86.     int nextPrime = 2;
  87.  
  88.     while(numberOfTries--) {
  89.         while(!isPrime(nextPrime)) ++nextPrime;
  90.         if(isPerfectSquare(number, nextPrime) == false) isFib = false;
  91.         nextPrime++;
  92.     }
  93.  
  94.     cout << (isFib ? "SIM" : "NAO") << '\n';
  95. }
  96.  
  97. int32_t main() {
  98.     fastio;
  99.  
  100.     int t = 1;
  101.     // std::cin >> t;
  102.     for(int caso = 1; caso <= t; ++caso) {
  103.         // cout << "Case #" << caso << ": ";
  104.         solve();
  105.     }  
  106.  
  107.     return 0;
  108. }
  109.  
  110. /*
  111. Before submit:
  112.     Check the corners cases
  113.     Check solution restrictions
  114.  
  115. For implementation solutions:
  116.     Check the flow of the variables
  117.  
  118. For intervals problems:
  119.     Think about two pointers
  120.  
  121. For complete search:
  122.     Think about cuts
  123.  
  124. If you get WA:
  125.     Reread your code looking for stupid typos
  126.     Try various manual testcases
  127.     Recheck the correctness of your algorithm
  128.     Reread the statement
  129.     Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
  130.     Generate random test cases and perform a stress test, comparing your solution with the one that you are sure is correct
  131.     Change the coder (if you're with a team)
  132.     Give up. You may have other tasks to solve.
  133. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement