Tranvick

Untitled

Feb 10th, 2012
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:65777216")
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <set>
  11. #include <stack>
  12. #include <map>
  13. #include <queue>
  14. #include <string>
  15. #include <memory.h>
  16. #include <iterator>
  17. #define mem(A,X) memset(A,X,sizeof(A))
  18. #define memo(A) memset(A,0,sizeof(A))
  19. #define forn(I,B) for (int I=1;I<=(B);I++)
  20. #define forg(H,V) for (int H=first[V];h;h=next[H])
  21. #define rep(I,B) for (int I=0;I<(B);I++)
  22. #define labs(X) (((X)>0)?(X):(-(X)))
  23. #define ropen(X) freopen(X,"r",stdin)
  24. #define wopen(X) freopen(X,"w",stdout)
  25. #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
  26. #define pb push_back
  27. #define mp make_pair
  28. #define all(X) (X).begin(),(X).end()
  29. #define sqr(X) ((X)*(X))
  30.  
  31. using namespace std;
  32.  
  33. typedef pair <int,int> pii;
  34. typedef long double ld;
  35. typedef long long  ll;
  36. typedef pair <ll,ll> pll;
  37. typedef vector<int> vi;
  38. const int N=111111;
  39. const int INF=1111111;
  40. const double eps=1e-9;
  41. const double pi=3.14159265358979;
  42.  
  43. ll n,m;
  44.  
  45. struct matrix{
  46.     ll a,b,c,d;
  47.     matrix(int a,int b,int c,int d):a(a),b(b),c(c),d(d){}
  48.     matrix(){}
  49.     inline ll mulmod(ll x,ll y){
  50.         ll res=0;
  51.         while (y){
  52.             if (y&1) res=(res+x)%m;
  53.             x=(x+x)%m;
  54.             y>>=1;
  55.         }
  56.         return res;
  57.     }
  58.     inline matrix operator * (const matrix & B){
  59.         matrix res;
  60.         res.a=(mulmod(a,B.a)+mulmod(b,B.c))%m;
  61.         res.b=(mulmod(a,B.b)+mulmod(b,B.d))%m;
  62.         res.c=(mulmod(c,B.a)+mulmod(d,B.c))%m;
  63.         res.d=(mulmod(c,B.b)+mulmod(d,B.d))%m;
  64.         return res;
  65.     }
  66. };
  67.  
  68. matrix fib(ll n){
  69.     matrix a(0,1,1,1),res(1,0,0,1);
  70.     while (n){
  71.         if (n&1) res=res*a;
  72.         a=a*a;
  73.         n>>=1;
  74.     }
  75.     return res;
  76. }
  77.  
  78. int main(){
  79.     cin>>n>>m;
  80.     matrix c=fib(n-1);
  81.     cout<<(c.a+c.c)%m;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment