Feel a day to live very fast, but I do not have any harvest.
1. Before just looking at the same as fast power multiplication to calculate a large number of multiplication mode, to prevent overflow, feel very useful, and used quite a lot.
2. The ability to analyze the problem is still very poor, encountered a problem, can not be correctly converted, how to consider, feel that this aspect is still very deficient, this should be through a lot of problems, and then constantly summed up the bar! After all, the problem does more, meet the new problem also on those kinds of routines. Feeling is quite right, the face of those small routines in the competition in front of the person is nothing, feel this sentence quite reasonable.
3. The problem of this time is to convert to the nth Fibonacci number, which I have not deduced at all. Then, before doing how to quickly solve, one is based on the recursive formula, but also want to be f (n) can be F (N/2), F (N/2 + 1), F (N/2-1) The number of the export, I also do not bother to remember, when encountered to take the first few numbers deduction, very simple. Another method is to transfer the matrix and quickly solve the power. This method is a general method (routines), most of the problems, to get out of the recursive formula, it is possible to transfer the condition is more, then make the form of transfer matrix, give the initial value, then the nth, is to find the matrix n power, and then is the fast power multiplication. This method is very important.
4. Forget, this is still a bit very important, is leetcode above house robber that problem, the idea is not quite difficult, but feel oneself write code is ugly, then today encounter similar conversion, it is very troublesome. Then look at the solution and come up with an ingenious method. The topic requirements can not be a continuous 1, because it is annular, the first and the last can not be 1, which adds complexity, you can not simply a recursive, and then consider this, the enumeration of the first position is 0, and then from the second position, arbitrarily put, can release vessel can not put, The final answer is the sum of the last put and not put, the second case is: the first put, then the second can not be put, and then from the third start, you can freely put, and then into the front of the problem, and do not have to repeat the calculation, the final answer is the last one can not put the number, Finally, the final answer is to add these two things together. So the code is written out and looks very neat. Think about it, it's really ingenious!
Here's what I wrote to ask for Fibonacci:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 Const intMAXN = 1e6 +Ten;5 Const intMoD = 1e9 +7;6ll dp[maxn][2];7 ll N;8Map<ll, ll>Ma;9 ll work (ll x) {Ten if(x = =0)return 1; One if(X <4)returnx; A if(Ma.count (x))returnMa[x]; -ll res =0; -ll t = x/2; the if(X &1) { -res = Work (t) * (Work (T-1) + Work (T +1)) %MoD; -}Else { -res = Work (t) * work (t)% mod + work (T-1) * Work (T-1) %MoD; + } - returnMA[X] =Res; + } A voidsolve () { atCIN >>N; -ll res = (work (n) + work (N-2)) %MoD; -cout << Res <<Endl; - } - intMain () { - solve (); in /*Enter your code here. Read input from STDIN. Print output to STDOUT*/ - return 0; to}
1#include <bits/stdc++.h>2 #definePB Push_back3 #definefor (i, n) for (int i = 0; i < (int) n; ++i)4 #defineDBG (x) cout << #x << "at line" << __line__ << ' is: ' << x << Endl5typedefLong Longll;6 using namespacestd;7typedef pair<int,int>PII;8 Const intMAXN = 1e3 +Ten;9 Const intMoD = 1e9 +7;Ten structMat { Onell a[4]; A voidinit () { -a[0] = a[3] =1; -a[1] = a[2] =0; the } -Mat Mul (ConstMat &x) { - Mat Res; -res.a[0] = (a[0] * x.a[0]% mod + a[1] * x.a[2]% MoD)%MoD; +res.a[1] = (a[0] * x.a[1]% mod + a[1] * x.a[3]% MoD)%MoD; -res.a[2] = (a[2] * x.a[0]% mod + a[3] * x.a[2]% MoD)%MoD; +res.a[3] = (a[2] * x.a[1]% mod + a[3] * x.a[3]% MoD)%MoD; A returnRes; at } - }; - Mat Pow (Mat A, ll b) { - Mat Res; - res.init (); - while(b) { in if(B &1) res =Res.mul (a); -A =A.mul (a); tob >>=1; + } - returnRes; the } * LL FB (ll N) { $ if(n = =0)return 1;Panax Notoginseng if(N <4)returnN; - Mat T; thet.a[0] =0; t.a[1] =1; t.a[2] =1; t.a[3] =1; +t =Pow (t, n); A returnt.a[3]; the } + ll N; - voidsolve () { $ while(Cin >>N) { $ -ll res = (FB (n) + FB (N-2)) %MoD; -cout << Res <<Endl; the } - }Wuyi intMain () { the //freopen ("test.in", "R", stdin); - //freopen ("Test.out", "w", stdout); Wu solve (); - return 0; About}
Fast calculation of Fibonacci sequences