Advertisement
istinishat

Matrix Multiplication Template

Feb 19th, 2019
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //link : https://codeforces.com/contest/1117/submission/50110620
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. const int SIZE=105;
  8. const int mod=(int)1e9+7;
  9.  
  10. struct Mat{
  11.     ll v[SIZE][SIZE];
  12.     Mat(){
  13.         memset(v,0,sizeof(v));
  14.     }
  15. };
  16.  
  17. Mat mulmat(Mat &a,Mat &b,int n=SIZE)
  18. {
  19.     Mat ret;
  20.     int i,j,k;
  21.     for(i=1;i<=n;i++){
  22.         for(j=1;j<=n;j++){
  23.             for(k=1;k<=n;k++){
  24.                 ret.v[i][j]=(ret.v[i][j]+(a.v[i][k]*b.v[k][j])%mod)%mod;
  25.             }
  26.         }
  27.     }
  28.     return ret;
  29.  
  30. }
  31.  
  32. Mat powmat(Mat a,ll p,int n=SIZE)
  33. {
  34.     Mat ret;
  35.     for(int i=1;i<=n;i++)ret.v[i][i]=1;
  36.     while(p){
  37.         if(p%2)ret=mulmat(ret,a,n);
  38.         a=mulmat(a,a,n);
  39.         p>>=1;
  40.     }
  41.     return ret;
  42. }
  43.  
  44. void print(Mat a,int n=SIZE)
  45. {
  46.     for(int i=1;i<=n;i++){
  47.         for(int j=1;j<=n;j++)
  48.             cout<<a.v[i][j]<<' ';
  49.         cout<<endl;
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     ll n=3,k=2;
  56.     int i,j;
  57.     cin>>n>>k;
  58.     if(n<k)return cout<<1<<endl,0;
  59.  
  60.     Mat now;
  61.     for(i=1;i<k;i++)now.v[i][i+1]=1;
  62.     now.v[1][1]=now.v[k][1]=1;
  63.  
  64.     now=powmat(now,n-k+1,k);
  65.     //print(now,k);
  66.     ll ans=0;
  67.     for(i=1;i<=k;i++)ans=(ans+now.v[i][1])%mod;
  68.  
  69.     cout<<ans<<endl;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement