fork download
  1. #include <bits/stdc++.h>
  2. #define FNAME "CANDY"
  3. using namespace std;
  4. const int MAXN = 501;
  5. typedef long long ll;
  6. const int MOD = 1e9 + 7;
  7.  
  8. void fastip() {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. if (fopen(FNAME".inp", "r")) {
  12. freopen(FNAME".inp", "r", stdin);
  13. freopen(FNAME".out", "w", stdout);
  14. }
  15. }
  16.  
  17. long long n;
  18.  
  19. struct Matrix{
  20. int n , m;
  21. vector<vector<long long>> f;
  22.  
  23. Matrix(int N, int M){
  24. n = N;
  25. m = M;
  26. if(N > 0 && M > 0){
  27. f.resize(n, vector<long long>(m , 0));
  28. }
  29. }
  30.  
  31. void Unit(){
  32. for(int i = 0; i < n ; i++){
  33. f[i][i] = 1;
  34. }
  35. }
  36.  
  37. Matrix operator * (const Matrix &other) const {
  38. Matrix res(n , other.m);
  39. for(int i = 0; i < n ; i++){
  40. for(int j = 0; j < other.m ; j++){
  41. long long total = 0;
  42. for(int k = 0; k < m ; k++){
  43. total = (total + f[i][k] * other.f[k][j]) % MOD;
  44. }
  45. res.f[i][j] = total % MOD;
  46. }
  47. }
  48. return res;
  49. }
  50. };
  51.  
  52. Matrix to_Unit(2, 2);
  53.  
  54. Matrix Pow(Matrix t, long long index){
  55. if(!index) return to_Unit;
  56. Matrix tmp = Pow(t, index / 2);
  57. if(index % 2 == 0){
  58. return tmp * tmp;
  59. }
  60. return tmp * tmp * t;
  61. }
  62.  
  63. int main(){
  64. fastip();
  65. to_Unit.Unit();
  66.  
  67. cin >> n;
  68.  
  69. Matrix T(2 , 2);
  70.  
  71. T.f = {
  72. {1 , 1},
  73. {1 , 0}
  74. };
  75.  
  76. Matrix dp(2, 1);
  77.  
  78. dp.f = {
  79. {1},
  80. {0}
  81. };
  82.  
  83. Matrix res = Pow(T, n - 1);
  84. res = res * dp;
  85.  
  86. if(n == 0) cout << 0;
  87. else cout << res.f[0][0];
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty