题目链接:Happy Necklace
题目大意:给你一个长度为n的字符串,只包含0和1,要求这个字符串的所有素数长度的子序列都得是1的个数比0的个数多,问长度为n的这样字符串有多少个
题目思路:先xjb推公式,公式推出来是f(n) = f(n-1)+f(n-3),然后做矩阵快速幂就好。
原矩阵:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3;
const int MOD = 1e9+7;
#define mod(x) ((x)%MOD)
ll n;
struct mat{
ll m[maxn][maxn];
}unit;
mat operator * (mat a,mat b){
mat ret;
ll x;
for(ll i = 0;i < 3;i++){
for(ll j = 0;j < 3;j++){
x = 0;
for(ll k = 0;k < 3;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 < maxn;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,t;
init_unit();
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
if(n == 2) puts("3");
else if(n == 3) puts("4");
else if(n == 4) puts("6");
else{
mat a,b;
b.m[0][0] = 1,b.m[0][1] = 1,b.m[0][2] = 0;
b.m[1][0] = 0,b.m[1][1] = 0,b.m[1][2] = 1;
b.m[2][0] = 1,b.m[2][1] = 0,b.m[2][2] = 0;
a.m[0][0] = 6,a.m[0][1] = 4,a.m[0][2] = 3;
a.m[1][0] = 0,a.m[1][1] = 0,a.m[1][2] = 0;
a.m[2][0] = 0,a.m[2][1] = 0,a.m[2][2] = 0;
b = pow_mat(b,n-4);
a = a*b;
printf("%lld\n",a.m[0][0]%MOD);
}
}
return 0;
}