题解 P4910 帕秋莉的手环

题面
如果是一条链的话,我们直接搞个转移矩阵,矩阵乘法即可
破环为链,并硬点一个端点黄色/绿色,然后讨论一下即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
struct matrix {
ll a[2][2];
friend inline matrix operator * (matrix a, matrix b) {
matrix ans;
memset(&ans, 0, sizeof ans);
for(int i = 0; i < 2; i++)
for(int k = 0; k < 2; k++)
for(int j = 0; j < 2; j++) ans[i][j] = (ans[i][j] + 1ll * a[i][k] * b[k][j]) % mod;
return ans;
}
ll* operator [] (ll x) {return a[x];}
} res, ans;
int main() {
int t;
long long n;
scanf("%d", &t);
while(t--) {
scanf("%lld", &n);
ans[0][0] = ans[1][1] = 1;
ans[0][1] = ans[1][0] = 0;
res[0][0] = res[0][1] = res[1][0] = 1;
res[1][1] = 0;
for(n--; n; n >>= 1, res = res * res) if(n & 1) ans = ans * res;
printf("%lld\n", ((ans[0][0] + ans[0][1]) % mod + ans[1][0]) % mod);
}
return 0;
}