【LOJ】#3086. 「GXOI / GZOI2019」逼死强迫症
LOJ#3086. 「GXOI / GZOI2019」逼死强迫症
这个就是设状态为\(S,j\)表示轮廓线为\(S\),然后用的1×1个数为j
列出矩阵转移
这样会算重两个边相邻的,只要算出斐波那契数然后乘上N就是不合法的方案
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
const int MOD = 1000000007;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int getid(int x,int y) {
return y * 4 + x;
}
template<int T>
struct Matrix {
int f[T][T];
Matrix(){memset(f,0,sizeof(f));}
friend Matrix operator * (const Matrix &a,const Matrix &b) {
Matrix c;
for(int i = 0 ; i < T ; ++i) {
for(int j = 0 ; j < T; ++j) {
for(int k = 0 ; k < T ; ++k) {
update(c.f[i][j],mul(a.f[i][k],b.f[k][j]));
}
}
}
return c;
}
friend Matrix fpow(Matrix a,int c) {
Matrix t = a,res;
for(int i = 0 ; i < T ; ++i) res.f[i][i] = 1;
while(c) {
if(c & 1) res = res * t;
t = t * t;
c >>= 1;
}
return res;
}
};
Matrix<12> a,ansa;
Matrix<2> b,ansb;
void Init() {
for(int j = 0 ; j <= 2 ; ++j) {
update(a.f[getid(0,j)][getid(0,j)],1);
update(a.f[getid(3,j)][getid(0,j)],1);
update(a.f[getid(0,j)][getid(3,j)],1);
}
update(a.f[getid(0,0)][getid(0,2)],1);
for(int j = 0 ; j < 2 ; ++j) {
update(a.f[getid(0,j)][getid(1,j + 1)],1);
update(a.f[getid(0,j)][getid(2,j + 1)],1);
update(a.f[getid(2,j)][getid(0,j + 1)],1);
update(a.f[getid(1,j)][getid(0,j + 1)],1);
}
for(int j = 0 ; j <= 2 ; ++j) {
update(a.f[getid(2,j)][getid(1,j)],1);
update(a.f[getid(1,j)][getid(2,j)],1);
}
update(b.f[0][0],1);update(b.f[0][1],1);
update(b.f[1][0],1);
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
int T,N;read(T);
for(int i = 1 ; i <= T ; ++i) {
read(N);
ansa = fpow(a,N);ansb = fpow(b,N);
out(inc(ansa.f[getid(0,0)][getid(0,2)],MOD - mul(N,ansb.f[0][0])));enter;
}
}