HDU 4549 M Fibonacci series (Rapid matrix power, rapid power reduction)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4549


F [0] = a ^ 1 * B ^ 0% P, F [1] = a ^ 0 * B ^ 1% P, f [2] = a ^ 1 * B ^ 1% p ..... f [N] = a ^ fib [n-1] * B ^ fib [N-2] % P.


Here, P is a prime number and a and P are mutually Prime, So we calculate a ^ B % P. When B is very large, we need to reduce the power of B.

Because a and P are mutually unique, a ^ (p-1) % P = 1 is known by the Fermat theorem. Make B = K * (PM) + B ', a ^ B % P = a ^ (K * (PM) + B ') % P = a ^ (K P-1) % P * a ^ B '% P

= A ^ B '% P. Here, p' = B % (p-1 ).

Then, the FIB [n-1] and FIB [N-2] of the above question can be obtained through the matrix Rapid power pair P-1) modulo, realizing power reduction and then using the rapid power.


In fact, a ^ B % C is a ^ (B % Phi [c] + Phi [c]) % C (B> = Phi [c]), when C is a prime number or A and C are mutually Prime, it can be simplified to a ^ (B % (c-1) % C.


At noon, the result was that the FIB series was wrong. F [0] = 0, F [1] = 1, F [N] = f [n-1] + F [N-2] (n> = 2 ). The 0th items are 0, sad ....


#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)#define C 240#define S 20using namespace std;const int maxn = 110;const int mod = 1000000007;struct matrix{LL mat[3][3];void init(){memset(mat,0,sizeof(mat));for(int i = 0; i < 2; i++)mat[i][i] = 1;}}m;LL a,b,n;matrix mul_matrix(matrix x, matrix y){matrix ans;memset(ans.mat,0,sizeof(ans.mat));for(int i = 0; i < 2; i++){for(int k = 0; k < 2; k++){if(x.mat[i][k] == 0) continue;for(int j = 0; j < 2; j++)ans.mat[i][j] = (ans.mat[i][j] + x.mat[i][k]*y.mat[k][j])%(mod-1);}}return ans;}matrix pow_matrix(matrix m, LL n){matrix ans;ans.init();while(n){if(n&1)ans = mul_matrix(ans,m);m = mul_matrix(m,m);n >>= 1;}return ans;}LL pow_mod(LL a, LL b){LL res = 1;a = a%mod;while(b){if(b&1)res = res*a%mod;a = a*a%mod;b >>= 1;}return res;}int main(){while(~scanf("%lld %lld %lld",&a,&b,&n)){m.mat[0][0] = m.mat[0][1] = m.mat[1][0] = 1;m.mat[1][1] = 0;if(n == 0){printf("%lld\n",a%mod);continue;}if(n == 1){printf("%lld\n",b%mod);continue;}else if(n == 2){printf("%lld\n",a*b%mod);continue;}matrix ans = pow_matrix(m,n);LL res = pow_mod(a,ans.mat[1][1]) * pow_mod(b,ans.mat[0][1]) %mod;printf("%lld\n",res);}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.