hdu 6050Funny Function矩阵快速幂


Funny Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1441    Accepted Submission(s): 732


Problem Description
Function  Fx,y satisfies:

For given integers N and M,calculate  Fm,1  modulo 1e9+7.
 

Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
 

Output
For each given N and M,print the answer in a single line.
 

Sample Input
  
  
2 2 2 3 3
 

Sample Output
  
  
2 33
 

Source
题解:根据给定的公式推导出:


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const int N = 4;
ll result[N][N];
ll temp[N][N];
ll A[N][N], B0[N][N], B1[N][N], tempA[N][N], B[N][N];
ll n, m;
void inIt() {
    A[1][1] = 0;A[1][2] = 2;
    A[2][1] = 1;A[2][2] = 1;
    B0[1][1] = 1;B0[1][2] = 0;
    B0[2][1] = 0;B0[2][2] = 1;
    B1[1][1] = -1;B1[1][2] = 2;
    B1[2][1] = 1;B1[2][2] = 0;
    tempA[1][1] = 1;tempA[1][2] = 0;
    tempA[2][1] = 0;tempA[2][2] = 1;
    result[1][1] = 1;result[1][2] = 0;
    result[2][1] = 0;result[2][2] = 1;
}
void updateA() {
    memset(temp, 0, sizeof(temp));
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j <= 2; j++) {
            for(int k = 1; k <= 2; k++) {
                temp[i][j] += A[i][k] * A[k][j];
                temp[i][j] %= INF;
            }
        }
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j <=2; j++)
            A[i][j] = temp[i][j];
}
void updateResultA() {
    memset(temp, 0, sizeof(temp));
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j <= 2; j++) {
            for(int k = 1; k <= 2; k++) {
                temp[i][j] += tempA[i][k] * A[k][j];
                temp[i][j] %= INF;
            }
        }
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j <=2; j++)
            tempA[i][j] = temp[i][j];
}
void fastMartixA() {
    ll k = n;
    while(k) {
        if(k&1)
            updateResultA();
        k >>= 1;
        updateA();
    }
}
void fastMartixB(ll a[][N]) {
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j <= 2; j++)
            B[i][j] = tempA[i][j]-a[i][j];
}
void updateResult(){
    memset(temp, 0, sizeof(temp));
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j<= 2; j++) {
            for(int k = 1; k <= 2; k++)
            temp[i][j] = (temp[i][j]+result[i][k]*B[k][j]+INF)%INF;
        }
    for(int i = 1; i<= 2; i++)
        for(int j = 1; j <= 2; j++)
            result[i][j] = temp[i][j];
}
void update() {
    memset(temp, 0, sizeof(temp));
    for(int i = 1; i <= 2; i++)
        for(int j = 1 ;  j <= 2; j++) {
            for(int k = 1; k<= 2; k++) {
                temp[i][j] = (temp[i][j]+B[i][k] * B[k][j]+INF)%INF;
            }
        }
    for(int i = 1; i <= 2; i++)
        for(int j = 1; j<= 2; j++)
            B[i][j] = temp[i][j];
}
void fastMartix() {
    ll k = m-1;
    while(k) {
        if(k&1) {
            updateResult();
        }
        k >>= 1;
        update();
    }
}
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld%lld", &n, &m);
        inIt();
        fastMartixA();
        if(n%2) {
            fastMartixB(B1);
            fastMartix();
            printf("%lld\n", (result[1][1]+result[2][1])%INF);
        }
        else {
            fastMartixB(B0);
            fastMartix();
            printf("%lld\n", (result[1][1]+result[2][1])%INF);

        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值