Algorithm learning, algorithm Learning Website

Source: Internet
Author: User

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.