题意

有nnn个点,每个点只能走到编号在[1,min(n+m,1)][1,min(n+m,1)][1,min(n+m,1)]范围内的点。求路径长度恰好为kkk的简单路径(一个点最多走一次)数。

1≤n≤109,1≤m≤4,1≤k≤min(n,12)1\le n\le 10^9,1\le m\le 4,1\le k\le min(n,12)1≤n≤109,1≤m≤4,1≤k≤min(n,12)

分析

直接考虑走路径的话不能判有没有走过,然后就把路径转化为一个序列,每次往里面插入新的点(神了)。因为一个点可以走到比他小的所有点,那么我们把点从大到小插入。

假设现在已有序列为p1,p2,p3,...,pkp_1,p_2,p_3,...,p_kp1​,p2​,p3​,...,pk​。那么当前插入一个点iii。

假设插在pjp_jpj​和pj+1p_{j+1}pj+1​之间,必须满足pjp_jpj​能走到iii并且iii能走到pj+1p_{j+1}pj+1​。由于iii是最小的,那么所有pjp_jpj​都能走到iii,所以只用考虑iii能走到哪些点。

  • 一种情况是直接放在最后。
  • 另一种情况是i+m≥pj+1i+m\ge p_{j+1}i+m≥pj+1​。那么满足这个式子的pj+1p_{j+1}pj+1​最多有m(≤4)m(\le 4)m(≤4)个。那么就把[i+1,i+m][i+1,i+m][i+1,i+m]这mmm个数有没有出现在序列中过状压成mmm位222进制数,记为SSS。当前方案就是bitcount(S)bitcount(S)bitcount(S)(SSS在222进制中有多少个111)。

那么一共就有bitcount(S)+1bitcount(S)+1bitcount(S)+1种方案。

另外还可以不插入。

所以DPDPDP状态设为f[i][j][S]f[i][j][S]f[i][j][S]表示到iii这个点,序列长度为jjj,上述状态为SSS的方案数,转移方程为:

f[i+1][j+1][(S<<1∣1)&(2m−1)]+=f[i][j][S]∗(bitcount(S)+1)f[i+1][j][(S<<1∣0)&(2m−1)]+=f[i][j][S]\begin{aligned}
f[i+1][j+1][(S<<1|1)\&(2^m-1)]&+=f[i][j][S]*(bitcount(S)+1)\\
f[i+1][j][(S&lt;&lt;1|0)\&amp;(2^m-1)]&amp;+=f[i][j][S]\end{aligned}f[i+1][j+1][(S<<1∣1)&(2m−1)]f[i+1][j][(S<<1∣0)&(2m−1)]​+=f[i][j][S]∗(bitcount(S)+1)+=f[i][j][S]​

由于nnn比较大,就矩阵加速就行了。这个矩阵快速幂还是挺好写的。。

CODE

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int mod = 1e9 + 7;
int n, k, m, all;
inline void add(int &x, int y) { x += y; if(x >= mod) x -= mod; }
struct mat {
int a[210][210]; //最大状态数为(k+1)*(1<<m)<=(12+1)*(2^4)=208
mat() { memset(a, 0, sizeof a); }
inline mat operator *(const mat &o)const {
mat re;
for(int k = 0; k < all; ++k)
for(int i = 0; i < all; ++i) if(a[i][k])
for(int j = 0; j < all; ++j) if(o.a[k][j])
add(re.a[i][j], 1ll * a[i][k] * o.a[k][j] % mod);
return re;
}
inline mat operator ^(int b)const {
mat re, A = *this;
for(int i = 0; i < all; ++i) re.a[i][i] = 1;
while(b) {
if(b & 1) re = re * A;
A = A * A; b >>= 1;
}
return re;
}
};
inline int enc(int K, int S) { return K*(1<<m) + S; }
inline int nxt(int S, bool x) { return ((S<<1)|x) & ((1<<m)-1); }
int cnt[16];
int main () {
scanf("%d%d%d", &n, &k, &m);
all = (k+1)*(1<<m); //所有状态数
mat trans, ans;
ans.a[0][enc(0, 0)] = 1;
for(int s = 1; s < (1<<m); ++s) cnt[s] = cnt[s>>1] + (s&1); //预处理2进制下有多少个1
for(int i = 0; i <= k; ++i)
for(int s = 0; s < (1<<m); ++s) {
if(i < k) trans.a[enc(i, s)][enc(i+1, nxt(s, 1))] = cnt[s]+1;
trans.a[enc(i, s)][enc(i, nxt(s, 0))] = 1;
}
ans = ans * (trans ^ n);
int Ans = 0;
for(int s = 0; s < (1<<m); ++s)
add(Ans, ans.a[0][enc(k, s)]);
printf("%d\n", (Ans + mod) % mod);
}

Codeforces Round #554 (Div. 2) F2. Neko Rules the Catniverse (Large Version) (矩阵快速幂 状压DP)的更多相关文章

  1. CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...

  2. Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...

  3. Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...

  4. Codeforces Round #554 (Div. 2) C. Neko does Maths (简单推导)

    题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k     算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出 ...

  5. Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

    题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0&l ...

  6. Codeforces Round #554 (Div. 2) C. Neko does Maths(数学+GCD)

    传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看 ...

  7. Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)

    传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...

  8. Codeforces Round #554 (Div. 2) C. Neko does Maths (数论 GCD(a,b) = GCD(a,b-a))

    传送门 •题意 给出两个正整数 a,b: 求解 k ,使得 LCM(a+k,b+k) 最小,如果有多个 k 使得 LCM() 最小,输出最小的k: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...

  9. Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))

    就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...

随机推荐

  1. yum源出问题,rpmdb: BDB0113 Thread/process 17276/140338032428864 failed: BDB1507 Thread died in Berkeley DB library

    yum源出问题 cd /var/lib/rpm rm -f *db.* rpm --rebuilddb 重构了之后就可以用了

  2. ORA-01078: failure in processing system parameters LRM-00109: could not open parameter file '/u01/app/oracle/product/19.2.0/db_1/dbs/initsanshi.ora'报错

    本人是在Linux安装Oracle19C之后,启动数据库时,XSHELL命令行窗口报的该错误,看了几个解决方案之后,总结如下 从字面的意思来看,是在dbs目录当中没有这个initsanshi.ora文 ...

  3. C++:标准模板库map

    一:介绍 map是STL的关联式容器,以key-value的形式存储,以红黑树(平衡二叉查找树)作为底层数据结构,对数据有自动排序的功能. 命名空间为std,所属头文件<map> 注意:不 ...

  4. python笔记007-函数

    昨日简要: 1.文件操作: 1.1获得句柄: f = open(‘one.txt’,mode=’’,encoding=’utf-8’) f = open(‘../’) à返回上一层 f = open( ...

  5. go变量的定义并赋值

    变量在定义时没有明确的初始化时会赋值为_零值_. 零值是: 数值类型为 `0`, 布尔类型为 `false`, 字符串为 `""`(空字符串). package main impo ...

  6. hdu 4496 其实还是并查集

    Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...

  7. web.xml 转 学习!http://www.cnblogs.com/wkrbky/p/5929943.html

    1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...

  8. Asp.Net Core 轻松学系列-1阅读指引目录

    https://www.cnblogs.com/viter/p/10474091.html 目录 前言 1. 从安装到配置 2. 业务实现 3. 日志 4. 测试 5. 缓存使用 6.网络和通讯 7. ...

  9. 【原创】大叔经验分享(89)docker启动openjdk执行jmap报错

    docker启动openjdk后,可以查看进程 # docker exec -it XXX jps 10 XXX.jar 可见启动的java进程id一直为10,然后可以执行jvm命令,比如 # doc ...

  10. HelenOS

    HelenOS 来源 http://www.helenos.org/ 关于HELENOS HelenOS是一种基于便携式微内核的多服务器操作系统,从头开始设计和实现.它将关键操作系统功能(如文件系统, ...