Algorithm learning, algorithm Learning Website
Quick power
As the name implies, a quick power is to quickly calculate the power of a certain number. The time complexity is O (log complexity N), which greatly improves the efficiency compared with the simple O (N.
Principle of quick power implementation
The principle of quick power is quite understandable, that is, if we want3^11In fact, the more common method is
For 1: 11
A * = 3;
The time complexity is O (n). Is there a faster way? Yes ~ The following is the power of the quick statement.
The quick power is to perform a log (N)-level transformation on the index.11 = 2^3+2^1+2^0
Then I only need to calculate3^1And3^2And3^8This reduces the complexity. Computing3^1It must be recordedaaSquare is3^2AsbbSquare is3^4AscAnd the second square is3^8AsdIn this waya b dMultiply by the result. This is faster ~
Quick power code
The Code is as follows:
This code is easy to understand because it is relatively simple. I wrote the following matrix power quickly, which may be difficult ~
int pow3(int a,int b){ int ans = 1,base = a; while(b!=0) { if(b&1) ans *= base; base *= base; b>>=1; } return ans;}
Rapid matrix power
Matrix power is a very interesting thing ~ Why? Because it can calculate the N-th feposer sequence of time at the Olog (N) level ~ It's easy ~ (What? You don't know what it is. Please Baidu ~ 1 1 2 3 5 8... series)
Implementation Principle
How is that implemented? First, we set up the ans matrix {1, 1; 1, 0}. Then this matrix is similar to the above3This base number, and then multiply the Matrix directly.
The febona sequence isF(n) = F(n-1) + F(n-2)The following code is a bit difficult,F(n) = a*F(n-1) + b*F(n-2)F (1) = F (2) = 1.
Implementation Code
The Code is as follows:
//// Main. cpp // numberSequence_hdu // Created by Alps on 14/12/22. // Copyright (c) 2014 chen. all rights reserved. // # include <iostream> using namespace std; void multiMatrix (int ma [] [2], int a, int B) {int I, j; int cp [2] [2] = {0, 0, 0}; for (I = 0; I <2; I ++) {for (j = 0; j <2; j ++) {cp [I] [j] = (ma [I] [0] * ma [0] [j]) % 7 + (ma [I] [1] * ma [1] [j]) % 7) % 7; // printf ("% d ", cp [I] [j]) ;}// printf ("\ n") ;}for (I = 0; I <2; I ++) {for (j = 0; j <2; j ++) {ma [I] [j] = cp [I] [j] ;}} void multiDoubleMatrix (int cp [] [2], int ma [] [2], int a, int B) {int temp [2] [2]; int I, j; for (I = 0; I <2; I ++) {for (j = 0; j <2; j ++) {temp [I] [j] = (cp [I] [0] * ma [0] [j]) % 7 + (cp [I] [1] * ma [1] [j]) % 7) % 7 ;}} for (I = 0; I <2; I ++) {for (j = 0; j <2; j ++) {cp [I] [j] = temp [I] [j] ;}} int calculate (int ma [] [2], int a, int B, int c) {if (c <= 0) {return 1 ;} int cp [2] [2] = {1, 0, 1}; while (c) {if (c & 1) {multiDoubleMatrix (cp, ma, a, B );} multiMatrix (ma, a, B); c = c> 1;} return (cp [0] [0] + cp [0] [1]) % 7 ;} int main (int argc, const char * argv []) {int a, B, c; while (1) {scanf ("% d", &, & B, & c); int ma [] [2] = {a % 7, B % 7, 1, 0 }; // printf ("% d \ n", ma [0] [0], ma [0] [1], ma [1] [0], ma [1] [1]); if (a = 0 & B = 0 & c = 0) {break ;} printf ("% d \ n", calculate (ma, a, B, C-2);} return 0 ;}