建立矩阵,跑快速幂

 /**************************************************************
Problem: 4000
User: idy002
Language: C++
Result: Accepted
Time:32 ms
Memory:836 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#define M 6 struct Matrix {
unsigned v[<<M][<<M];
const unsigned *operator[]( int i ) const { return v[i]; }
}; int n, m, p, k, bound;
int attack[];
int stat[<<M], id[<<M], stot;
Matrix base, dest; void make_unit( Matrix &x ) {
for( int i=; i<stot; i++ )
for( int j=; j<stot; j++ )
x.v[i][j] = i==j;
}
Matrix operator*( const Matrix &a, const Matrix &b ) {
Matrix c;
for( int i=; i<stot; i++ )
for( int j=; j<stot; j++ ) {
c.v[i][j] = ;
for( int k=; k<stot; k++ )
c.v[i][j] += a[i][k]*b[k][j];
}
return c;
}
Matrix mpow( Matrix a, int b ) {
Matrix rt;
for( make_unit(rt); b; b>>=,a=a*a )
if( b& ) rt=rt*a;
return rt;
}
int getarea( int s, int a ) {
int rt = ;
for( int b=; b<m; b++ ) {
if( (s>>b)& ) {
int aa = a;
if( b<k )
aa >>= k-b;
else
aa <<= b-k;
aa &= bound;
rt |= aa;
}
}
return rt;
}
void build() {
stot = ;
memset( id, -, sizeof(id) );
for( int s=; s<=bound; s++ ) {
if( getarea(s,attack[])&s ) continue;
stat[stot]=s;
id[s] = stot;
stot++;
}
for( int s1=; s1<=bound; s1++ ) {
if( id[s1]==- ) continue;
for( int s2=; s2<=bound; s2++ ) {
if( id[s2]==- ) continue;
if( getarea(s1,attack[])&s2 ) continue;
if( getarea(s2,attack[])&s1 ) continue;
base.v[id[s1]][id[s2]] = ;
}
}
}
int main() {
scanf( "%d%d%d%d", &n, &m, &p, &k );
bound = (<<m)-;
for( int i=; i<; i++ )
for( int j=,o; j<p; j++ ) {
scanf( "%d", &o );
attack[i] = attack[i] | (o<<j);
}
attack[] ^= <<k;
build();
dest = mpow( base, n- );
unsigned ans = ;
for( int i=; i<stot; i++ )
for( int j=; j<stot; j++ )
ans += dest[i][j];
printf( "%u\n", ans );
}

bzoj 4000 矩阵快速幂优化DP的更多相关文章

  1. 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)

    传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...

  2. 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)

    Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...

  3. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

  4. 2018.10.16 uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂优化dp)

    传送门 一道不错的矩阵快速幂优化dpdpdp. 设f[i][j][k][l]f[i][j][k][l]f[i][j][k][l]表示前iii轮第iii轮还有jjj个一滴血的,kkk个两滴血的,lll个 ...

  5. 【bzoj1009】[HNOI2008]GT考试(矩阵快速幂优化dp+kmp)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 这道题一看数据范围:$ n<=10^9 $,显然不是数学题就是矩乘快速幂优 ...

  6. BZOJ 1009 [HNOI2008]GT考试(矩阵快速幂优化DP+KMP)

    题意: 求长度为n的不含长为m的指定子串的字符串的个数 1s, n<=1e9, m<=50 思路: 长见识了.. 设那个指定子串为s f[i][j]表示长度为i的字符串(其中后j个字符与s ...

  7. LOJ2325. 「清华集训 2017」小 Y 和恐怖的奴隶主【矩阵快速幂优化DP】【倍增优化】

    LINK 思路 首先是考虑怎么设计dp的状态 发现奴隶主的顺序没有影响,只有生命和个数有影响,所以就可以把每个生命值的奴隶主有多少压缩成状态就可以了 然后发现无论是什么时候一个状态到另一个状态的转移都 ...

  8. bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...

  9. 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)

    传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...

随机推荐

  1. Spark记录-Spark on mesos配置

    1.安装mesos #用centos6的源yum安装 # rpm -Uvh http://repos.mesosphere.io/el/6/noarch/RPMS/mesosphere-el-repo ...

  2. Spark记录-官网学习配置篇(一)

    参考http://spark.apache.org/docs/latest/configuration.html Spark提供三个位置来配置系统: Spark属性控制大多数应用程序参数,可以使用Sp ...

  3. Google Email 帐户泄露

    最初爆出来的网站是:https://forum.btcsec.com/index.php?/topic/9426-gmail-meniai-parol/,是一个俄罗斯论坛,然后..就流传开来了... ...

  4. uva 10625 Board Wrapping

    https://vjudge.net/problem/UVA-10652 给出n个长方形,用一个面积尽量小的凸多边形把他们围起来 求木板占包装面积的百分比 输入给出长方形的中心坐标,长,宽,以及长方形 ...

  5. 《Linux命令行与shell脚本编程大全》 第五章理解shell

    5.1 1. cat /etc/passwd 可以查看每个用户自己的默认的shell程序. 2.默认的交互shell会在用户登录某个虚拟控制台终端时启动. 不过还有另外一个默认的shell是/bin/ ...

  6. 解决PHP curl https时error 77(Problem with reading the SSL CA cert (path? access rights?))

    服务器环境为CentOS,php-fpm,使用curl一个https站时失败,打开curl_error,捕获错误:Problem with reading the SSL CA cert (path? ...

  7. 在Emacs中启用Fcitx输入法

    安装fcitx输入法,在 ~/.xinitrc文件中添加如下内容 (我用startx启动图形环境,所以在~/.xinitrc中配置X会话) export LC_CTYPE="zh_CN.UT ...

  8. the error about “no such file or directory”

    CHENYILONG Blog the error about "no such file or directory" when you get the question like ...

  9. Linux - awk 文本处理工具一

    AWK AWK是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一:awk经过改进生成的新的版本nawk,gawk,现在默认linux系统下日常使用的是gawk,用命 ...

  10. plsql 用法和技巧

    1.导入csv文件 2.保存登录的密码