Blocks

http://poj.org/problem?id=3734

题解:矩阵快速幂

构造初始矩阵:

{0,1,0,1}
{0,0,0,0}
{0,0,0,0}
{0,0,0,0}

构造转移矩阵:

{3,1,3,1}
{1,3,1,3}
{0,0,0,0}
{0,0,0,0}

参考文章: 矩阵乘积    矩阵快速幂   矩阵构造方法   

/*
*@Author:   STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=10+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v,x,y;
int ans,cnt,flag,temp,sum;
int s[N][N];
int b[N][N];
int a[N][N];
char str;
struct node{};
void Matrix(ll a[N][N],ll b[N][N]){
    ll c[N][N];
    memset(c,0,sizeof(c));
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%10007;
            }
        }
    }
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            a[i][j]=c[i][j];
        }
    }
}
ll power(int k){
    ll b[N][N]={{3,1,3,1},
                {1,3,1,3},
                {0,0,0,0},
                {0,0,0,0}};
    ll a[N][N]={{0,1,0,1},
                {0,0,0,0},
                {0,0,0,0},
                {0,0,0,0}};
    while(k){
        if(k&1)Matrix(a,b);
        Matrix(b,b);
        k>>=1;
    }
    return (a[0][1]+a[0][3])%10007;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        cout<<power(n-1)<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-29 12:19
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务