洛谷 P1962 斐波那契数列

题目链接:https://www.luogu.org/problemnew/show/P1962

题目大意:

  略

分析:

  由于数据规模很大,需要用矩阵快速幂来解。

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define inI(x) scanf("%d", &x)
 15 #define inII(x, y) scanf("%d%d", &x, &y)
 16 #define inIII(x, y, z) scanf("%d%d", &x, &y, &z)
 17 #define inL(x) scanf("%lld", &x)
 18 #define outI(x) printf("%d", &x)
 19 #define outI_S(x) printf("%d ", &x)
 20 #define outL(x) printf("%lld", &x)
 21 #define outL_S(x) printf("%lld ", &x)
 22 #define outD(x) printf("%f", &x)
 23 #define outD_S(x) printf("%f ", &x)
 24 #define EL() printf("\n")
 25 
 26 #define pr(x) cout << #x << " = " << x << "  "
 27 #define pr(x) cout << #x << " = " << x << "  "
 28 #define pr(x) cout << #x << " = " << x << "  "
 29 #define prln(x) cout << #x << " = " << x << endl
 30  
 31 #define LOWBIT(x) ((x)&(-x))
 32  
 33 #define ALL(x) x.begin(),x.end()
 34 #define INS(x) inserter(x,x.begin())
 35  
 36 #define ms0(a) memset(a,0,sizeof(a))
 37 #define msI(a) memset(a,inf,sizeof(a))
 38 #define msM(a) memset(a,-1,sizeof(a))
 39 
 40 #define MP make_pair
 41 #define PB push_back
 42 #define ft first
 43 #define sd second
 44  
 45 template<typename T1, typename T2>
 46 istream &operator>>(istream &in, pair<T1, T2> &p) {
 47     in >> p.first >> p.second;
 48     return in;
 49 }
 50  
 51 template<typename T>
 52 istream &operator>>(istream &in, vector<T> &v) {
 53     for (auto &x: v)
 54         in >> x;
 55     return in;
 56 }
 57  
 58 template<typename T1, typename T2>
 59 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 60     out << "[" << p.first << ", " << p.second << "]" << "\n";
 61     return out;
 62 }
 63  
 64 typedef long long LL;
 65 typedef unsigned long long uLL;
 66 typedef pair< double, double > PDD;
 67 typedef pair< int, int > PII;
 68 typedef set< int > SI;
 69 typedef vector< int > VI;
 70 typedef map< int, int > MII;
 71 const double EPS = 1e-10;
 72 const int inf = 1e9 + 9;
 73 const LL mod = 1e9 + 7;
 74 const int maxN = 1e5 + 7;
 75 const LL ONE = 1;
 76 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 77 const LL oddBits = 0x5555555555555555;
 78 
 79 LL n, MOD = mod;
 80 
 81 struct Matrix{
 82     static const int sz = 2;
 83     LL mat[sz][sz];
 84     
 85     Matrix(){ ms0(mat); }
 86     Matrix(LL A[sz][sz]){
 87         Rep(i, sz) {
 88             Rep(j, sz) {
 89                 mat[i][j] = A[i][j];
 90             }
 91         }
 92     }
 93     Matrix(vector< vector< LL > > &A){
 94         Rep(i, sz) {
 95             Rep(j, sz) {
 96                 mat[i][j] = A[i][j];
 97             }
 98         }
 99     }
100     Matrix(const initializer_list< initializer_list< LL > > &A){
101         int i = 0, j = 0;
102         for (auto R : A) {
103             j = 0;
104             for (auto x : R) {
105                 mat[i][j] = x;
106                 ++j;
107             }
108             ++i;
109         }
110     }
111     
112     // x * 单位阵 
113     inline void E(int x = 1) {
114         ms0(mat); 
115         Rep(i, sz) mat[i][i] = x;
116     }
117     
118     inline Matrix operator = (const initializer_list< initializer_list< LL > > &A) const {
119         Matrix ret;
120         int i = 0, j = 0;
121         for (auto R : A) {
122             j = 0;
123             for (auto x : R) {
124                 ret.mat[i][j] = x;
125                 ++j;
126             }
127             ++i;
128         }
129         return ret;
130     }
131     
132     inline Matrix operator = (const Matrix x) {
133         Rep(i, sz) {
134             Rep(j, sz) {
135                 mat[i][j] = x.mat[i][j];
136             }
137         }
138         return *this;
139     }
140      
141     inline Matrix operator + (const Matrix &x) {
142         Matrix ret;
143         Rep(i, sz) {
144             Rep(j, sz) {
145                 ret.mat[i][j] = mat[i][j] + x.mat[i][j];
146                 ret.mat[i][j] %= MOD;
147             }
148         }
149         return ret;
150     }
151     
152     inline Matrix operator - (const Matrix &x) {
153         Matrix ret;
154         Rep(i, sz) {
155             Rep(j, sz) {
156                 ret.mat[i][j] = mat[i][j] - x.mat[i][j] + MOD;
157                 ret.mat[i][j] %= MOD;
158             }
159         }
160         return ret;
161     }
162     
163     inline Matrix operator * (const Matrix &x) {
164         Matrix ret;
165         Rep(i, sz) {
166             Rep(j, sz) {
167                 Rep(k, sz) {
168                     ret.mat[i][j] += mat[i][k] * x.mat[k][j];
169                     ret.mat[i][j] %= MOD;
170                 }
171             }
172         }
173         return ret;
174     }
175     
176     inline Matrix operator * (const LL &x) {
177         Matrix ret;
178         Rep(i, sz) {
179             Rep(j, sz) {
180                 ret.mat[i][j] = mat[i][j] * x;
181                 ret.mat[i][j] %= MOD;
182             }
183         }
184         return ret;
185     }
186     
187     inline Matrix operator += (const Matrix &x) { return *this = *this + x; }
188     inline Matrix operator -= (const Matrix &x) { return *this = *this - x; }
189     inline Matrix operator *= (const Matrix &x) { return *this = *this * x; }
190     inline Matrix operator *= (const LL &x) { return *this = *this * x; }
191     
192     inline void print() {
193         Rep(i, sz) {
194             Rep(j, sz) {
195                 cout << mat[i][j] << " ";
196             }
197             cout << endl;
198         }
199     }
200 };
201 
202 // 矩阵快速幂,计算x^y 
203 inline Matrix mat_pow_mod(Matrix x, LL y) {
204     Matrix ret;
205     ret.E();
206     while(y){
207         if(y & 1) ret *= x;
208         
209         x *= x;
210         y >>= 1;
211     }
212     return ret;
213 }
214 
215 Matrix X = {{1, 1}, {1, 0}};
216 Matrix ans;
217 
218 int main(){
219     cin >> n;
220     ans = mat_pow_mod(X, n);
221     if(n == 0) cout << 0 << endl;
222     else cout << ans.mat[0][1] << endl;
223     return 0;
224 }
View Code

猜你喜欢

转载自www.cnblogs.com/zaq19970105/p/10781305.html
今日推荐