Advertisement
erfanul007

Untitled

Dec 3rd, 2020
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <iostream>
  3. // #include <cstdio>
  4. // #include <cstdlib>
  5. // #include <algorithm>
  6. // #include <cmath>
  7. // #include <vector>
  8. // #include <set>
  9. // #include <map>
  10. // #include <queue>
  11. // #include <ctime>
  12. // #include <cassert>
  13. // #include <complex>
  14. // #include <string>
  15. // #include <cstring>
  16. // #include <queue>
  17. // #include <bitset>
  18.  
  19. using namespace std;
  20.  
  21. // #pragma GCC optimize("Ofast,no-stack-protector")n
  22. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  23. // #pragma GCC optimize("unroll-loops")
  24.  
  25.  
  26. #define ll              long long int
  27. #define ull             unsigned long long int
  28. #define vi              vector< int >
  29. #define vll             vector< ll >
  30.  
  31. #define sc              scanf
  32. #define pf              printf
  33. #define cspf(i)         pf("Case %d: ", i)
  34. #define spc             pf(" ")
  35. #define line            pf("\n")
  36.  
  37. #define ff              first
  38. #define ss              second
  39. #define mp              make_pair
  40. #define pb              push_back
  41. #define ppb             pop_back
  42. #define tp(v,j)         get<j>(v)
  43. #define Log(b,x)        (log(x)/log(b))
  44.  
  45. #define FOR(i,x,y)      for(int i = int(x); i < int(y); i++)
  46. #define ROF(i,x,y)      for(int i = int(x)-1; i >= int(y); i--)
  47. #define clr(arr,x)      memset(arr, x, sizeof arr)
  48. #define vout(v)         for(int w=0;w<(int)v.size();w++){if(w) spc; cout<<v[w];}
  49. #define all(v)          v.begin(), v.end()
  50. #define rall(v)         v.rbegin(), v.rend()
  51. #define unq(v)          sort(all(v)),(v).resize(unique(all(v))-v.begin())
  52. #define fastIO          ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
  53.  
  54. #define sc1(x)          sc("%d",&x)
  55. #define sc2(x,y)        sc("%d %d", &x, &y)
  56. #define sc3(x,y,z)      sc("%d %d %d", &x, &y, &z)
  57. #define scl1(x)         sc("%lld",&x);
  58. #define scl2(x,y)       sc("%lld %lld", &x, &y)
  59. #define scf1(x)         sc("%lf",&x)
  60. #define scf2(x,y)       sc("%lf %lf", &x, &y)
  61.  
  62. #define pf1(x)          pf("%d",x)
  63. #define pf2(x,y)        pf("%d %d", x, y)
  64. #define pf3(x,y,z)      pf("%d %d %d", x, y, z)
  65. #define pfl1(x)         pf("%lld",x)
  66. #define pfl2(x,y)       pf("%lld %lld",x,y)
  67.  
  68. // #define MOD             1000000007        
  69. #define MAX             100001
  70. #define inf             0x3f3f3f3f
  71. #define PI              acos(-1.0)  // 3.1415926535897932
  72. #define eps             1e-6
  73.  
  74. template <class T> inline T bigMod(T p,T e,T M){T ret=1; for(;e>0;e>>=1){ if(e&1) ret=(ret*p)%M; p=(p*p)%M;} return (T)ret;}
  75. template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
  76. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  77. template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  78. template <class T> inline T SQR(T a){return a*a;}
  79.  
  80. int dx[] = { 1,-1, 0, 0};                //graph moves
  81. int dy[] = { 0, 0, 1,-1};               //graph moves
  82.  
  83. // C++ program to find the Nth Fibonacci
  84. // number using Fast Doubling Method
  85.  
  86. int a, b, c, d;
  87.  
  88. // Function calculate the N-th fibanacci
  89. // number using fast doubling method
  90. void FastDoubling(int n, int res[], int MOD)
  91. {
  92.     // Base Condition
  93.     if (n == 0) {
  94.         res[0] = 0;
  95.         res[1] = 1;
  96.         return;
  97.     }
  98.     FastDoubling((n / 2), res, MOD);
  99.  
  100.     // Here a = F(n)
  101.     a = res[0];
  102.  
  103.     // Here b = F(n+1)
  104.     b = res[1];
  105.  
  106.     c = 2 * b - a;
  107.  
  108.     if (c < 0)
  109.         c += MOD;
  110.  
  111.     // As F(2n) = F(n)[2F(n+1) – F(n)]
  112.     // Here c  = F(2n)
  113.     c = (a * c) % MOD;
  114.  
  115.     // As F(2n + 1) = F(n)^2 + F(n+1)^2
  116.     // Here d = F(2n + 1)
  117.     d = (a * a + b * b) % MOD;
  118.  
  119.     // Check if N is odd
  120.     // or even
  121.     if (n % 2 == 0) {
  122.         res[0] = c;
  123.         res[1] = d;
  124.     }
  125.     else {
  126.         res[0] = d;
  127.         res[1] = c + d;
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     #ifndef ONLINE_JUDGE
  135.         clock_t tStart = clock();
  136.         freopen("input.txt", "r", stdin);
  137.         freopen("output.txt", "w", stdout);
  138.     #endif
  139.     int n, m;
  140.     while(cin>>n>>m){
  141.         int res[2] = { 0 };
  142.  
  143.         m = pow(2, m);
  144.         FastDoubling(n, res, m);
  145.  
  146.         cout << res[0]%m << "\n";
  147.  
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement