題意:一個只含字母C和P的環形串
求長度為n且每m個連續字符不含有超過k個C的方案數?
m <= 5? n <= 1e15
題解:用一個m位二進制表示狀態 轉移很好想
但是這個題是用矩陣快速冪加速dp的 因為每一位的轉移都是一樣的
用一個矩陣表示狀態i能否轉移到狀態j 然后跑一遍
統計答案特別講究 因為是一個環 從1 ~ n+m
那么 m+1 ~ n + m之間就是我們所求的 1 ~ m和n+1 ~ n + m是同樣的一段
就相當于把m位二進制狀態 轉移n次
然后再轉移到自己的就是答案
初試模板題
?


#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9 + 7;ll n, m, k, len; struct node {ll c[64][64]; }re, x;bool check(int x) {int cnt = 0;while(x) {if(x & 1) cnt++;x >>= 1;}if(cnt > k) return false;return true; }node mul(node a, node b) {node res;memset(res.c, 0, sizeof(res.c));for(int i = 0; i < len; i++)for(int j = 0; j < len; j++)for(int k = 0; k < len; k++)res.c[i][j] = (res.c[i][j] + a.c[i][k] * b.c[k][j] % mod) % mod;return res; }node pow_mod(node x, ll y) {node res;for(int i = 0; i < len; i++) res.c[i][i] = 1;while(y) {if(y & 1) res = mul(res, x);x = mul(x, x);y >>= 1;}return res; }int main() {scanf("%lld%lld%lld", &n, &m, &k);len = (1 << m);for(int i = 0; i < len; i++)for(int j = 0; j < len; j++)x.c[i][j] = 0;for(int i = 0; i < len; i++) {if(!check(i)) continue;int tmp = i;int ctmp = 1 << (m - 1);if((tmp & ctmp) == ctmp) tmp -= ctmp;tmp <<= 1;if(check(tmp)) x.c[i][tmp] = 1;tmp |= 1;if(check(tmp)) x.c[i][tmp] = 1;}re = pow_mod(x, n);ll ans = 0;for(int i = 0; i < len; i++) {if(check(i)) {ans += re.c[i][i];ans %= mod;}}printf("%lld\n", ans);return 0; }
?