Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK:65777216")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <stack>
- #include <map>
- #include <queue>
- #include <string>
- #include <memory.h>
- #include <iterator>
- #define mem(A,X) memset(A,X,sizeof(A))
- #define memo(A) memset(A,0,sizeof(A))
- #define forn(I,B) for (int I=1;I<=(B);I++)
- #define forg(H,V) for (int H=first[V];h;h=next[H])
- #define rep(I,B) for (int I=0;I<(B);I++)
- #define labs(X) (((X)>0)?(X):(-(X)))
- #define ropen(X) freopen(X,"r",stdin)
- #define wopen(X) freopen(X,"w",stdout)
- #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
- #define pb push_back
- #define mp make_pair
- #define all(X) (X).begin(),(X).end()
- #define sqr(X) ((X)*(X))
- using namespace std;
- typedef pair <int,int> pii;
- typedef long double ld;
- typedef long long ll;
- typedef pair <ll,ll> pll;
- typedef vector<int> vi;
- const int N=111111;
- const int INF=1111111;
- const double eps=1e-9;
- const double pi=3.14159265358979;
- ll n,m;
- struct matrix{
- ll a,b,c,d;
- matrix(int a,int b,int c,int d):a(a),b(b),c(c),d(d){}
- matrix(){}
- inline ll mulmod(ll x,ll y){
- ll res=0;
- while (y){
- if (y&1) res=(res+x)%m;
- x=(x+x)%m;
- y>>=1;
- }
- return res;
- }
- inline matrix operator * (const matrix & B){
- matrix res;
- res.a=(mulmod(a,B.a)+mulmod(b,B.c))%m;
- res.b=(mulmod(a,B.b)+mulmod(b,B.d))%m;
- res.c=(mulmod(c,B.a)+mulmod(d,B.c))%m;
- res.d=(mulmod(c,B.b)+mulmod(d,B.d))%m;
- return res;
- }
- };
- matrix fib(ll n){
- matrix a(0,1,1,1),res(1,0,0,1);
- while (n){
- if (n&1) res=res*a;
- a=a*a;
- n>>=1;
- }
- return res;
- }
- int main(){
- cin>>n>>m;
- matrix c=fib(n-1);
- cout<<(c.a+c.c)%m;
- }
Advertisement
Add Comment
Please, Sign In to add comment