题目链接:Covering
解题思路:递推+快速幂。
递推思路请看:HDU 6185 && 2017广西邀请赛:Covering(矩阵快速幂)
下面是我自己本题的快速幂代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
using namespace std;
typedef __int64 ll;
const int dim = 4;
const ll MOD = 1000000007;
#define mod(x) ((x)%MOD)
struct mat{
ll m[dim][dim];
}unit;
mat operator * (mat a,mat b){
mat ret;
ll x;
for(ll i = 0;i < dim;i++){
for(ll j = 0;j < dim;j++){
x = 0;
for(ll k = 0;k < dim;k++)
x += mod((ll)a.m[i][k]*b.m[k][j]);
ret.m[i][j] = mod(x);
}
}
return ret;
}
void init_unit(){
for(ll i = 0;i < dim;i++)
unit.m[i][i] = 1;
return ;
}
mat pow_mat(mat a,ll n){
mat ret = unit;
while(n){
if(n&1) ret = ret*a;
a = a*a;
n >>= 1;
}
return ret;
}
int main(){
ll n;
init_unit();
while(~scanf("%lld",&n)){
if(n == 1) printf("1\n");
else if(n == 2) printf("5\n");
else if(n == 3) printf("11\n");
else{
mat a,b;
b.m[0][0]=0,b.m[0][1]=1,b.m[0][2]=1,b.m[0][3]=0;
b.m[1][0]=5,b.m[1][1]=1,b.m[1][2]=0,b.m[1][3]=0;
b.m[2][0]=0,b.m[2][1]=0,b.m[2][2]=0,b.m[2][3]=1;
b.m[3][0]=1,b.m[3][1]=0,b.m[3][2]=0,b.m[3][3]=0;
a.m[0][0]=11,a.m[0][1]=7,a.m[0][2]=5,a.m[0][3]=1;
b = pow_mat(b,n-3);
a = a*b;
printf("%I64d\n",a.m[0][0]%MOD);
}
}
return 0;
}