FZU2018 level algorithm first job 1.1fibonacci (matrix fast power)

topic

  Winder recent knowledge in learning fibonacci sequence. We know that the number of columns fibonacci recursion formulas is F (n) = F (n -1) + F (n-2) (n> = 2 , and n is an integer). Winder we want to know is when to this recursive F. (N) = A F. (. 1-n) B + F. (N-2) (n> = 2, and n is an integer), we get what the number of columns. However, Winder lazy, so only up to you to help him to get this done. Note that here we still make F (0) = F (1 ) = 1.

★ data input

  Input of the first row three positive integers N, A, and B (N <= 10; 1 <= A, B <= 100 and are integers). Then there are N rows, each row a natural number n (n <= 100000000).

★ data output

  A line output integer F (n), as a result may be large, Winder required output of modulo 2013.

Input Example Output Example

5 4 5

2

4

8

16

32

9

209

1377

182

9

 

answer:

  A classic matrix bare rapid power problem.

  First, to explain the power of fast, when we request $ a ^ {b} $ mod modulus of time, b can be converted to binary, b can be converted into a number of second power of the sum. For example, when we calculate the 12 th power of 2, 12 is the binary bit weights of 1100, 1100 in two 4 1 and 8, respectively, so $ 2 ^ {12} $ times can easily be converted to $ 2 ^ {4} * $ 2 ^ {8}. Thus, we first assigned to the answer ans 1,2,4,8 ..... 1 power, only a calculation, and then look at the value of the bit b is 1 can be, if 1 , multiplied by the ans can.

Fast power code as follows

ll fastpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

  Next, return to this problem, since the larger the number n, the modulo plus slow, this question recursive recursive time out. We need to find the algorithm complexity is less than O (n) a.

  We can construct the matrix $ \ binom {f (n)} {f (n-1)} $, can be obtained

$$
\left(
\begin{matrix}
a & b \\
1 & 0
\end{matrix}
\right) \tag{2}^{n-1}*
\binom{f(1)}{f(0)}=\binom{f(n)}{f(n-1)}
$$

Thus, we only need to calculate the matrix

$$
\left(
\begin{matrix}
a & b \\
1 & 0
\end{matrix}
\right)
$$

The n-1 th power can, for this matrix the n-1 th power, the power obtained by using a fast seek matrix, we can calculate the value of f (n) in the $ \ log n $ time.

#include<iostream>
#include
<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<cstring> #include<string> #include<vector> #include<queue> #include<set> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair <int,int> pii; #define rep(i,x,y) for(int i=x;i<y;i++) #define rept(i,x,y) for(int i=x;i<=y;i++) #define per(i,x,y) for(int i=x;i>=y;i--) #define pb push_back #define fi first #define se second #define mes(a,b) memset(a,b,sizeof a) const int inf=0x3f3f3f3f; const int mod=2013; class matrix { public: int arrcy[6][6];//arrcy为矩阵,下表从0开始 int row,column;//row为矩阵的行,column为矩阵的列 friend matrix operator *(matrix s1,matrix s2) { int i,j; matrix s3; for (i=0;i<s1.row;i++) { for (j=0;j<s2.column;j++) { for (int k=0;k<s1.column;k++) { s3.arrcy[i][j]+=s1.arrcy[i][k]*s2.arrcy[k][j]; s3.arrcy[i][j]%=mod; } } } S3.row = s1.row; s3.column = s2.column; return S3; } }; Matrix quick_pow (Matrix S1, Long Long n-) // fast power function matrix, s1 matrix, n is the power of { matrix MUL = S1, ans;
// ans be configured as a matrix ans.row
= ans.column = s1.row; Memset (ans.arrcy, 0 , the sizeof ans.arrcy); for ( int I = 0 ; I <ans.row; I ++ ) ans.arrcy [I] [I] = . 1; while(n) { if(n&1) ans=ans*mul; mul=mul*mul; n/=2; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n,a,b; cin>>n>>a>>b; matrix mul; mul.row=mul.column=2; mul.arrcy[0][0]=a; mul.arrcy[0][1]=b; mul.arrcy[1][0]=1; mul.arrcy[1][1]=0; matrix r; r.row=2; r.column=1; r.arrcy[0][0]=r.arrcy[1][0]=1; rep(i,0,n) { int x; cin>>X; if(! X) // when x = 1, x-1 < 0 can not use fast power, the answer is 0, judgment can Laid { COUT << . 1 << endl; Continue ; } Matrix mm = quick_pow (MUL, X - . 1 ); mm = mm * R & lt; COUT << mm.arrcy [ 0 ] [ 0 ] << endl; } return 0 ; }

 

Guess you like

Origin www.cnblogs.com/FZUzyz/p/11490946.html