题目链接:http://poj.org/problem?id=3734

题意:给出n个排成一列的方块,用红、蓝、绿、黄四种颜色给它们染色,求染成红、绿的方块个数同时为偶数的方案数模10007的值。

题解:这是WC2019第二课堂生成函数的题,实际上可以不用生成函数,我们考虑如下状态:a[i]表示前i个中红、绿均为偶的方案数,b[i]表示其中一个为奇数的方案数,c[i]表示都为奇数的方案数。然后可以这样转移:a[i]=2a[i-1]+b[i-1],b[i]=2a[i-1]+2b[i-1]+2c[i-1],c[i]=2c[i-1]+b[i-1],由于n<=1e9,所以用矩阵优化即可,复杂度O(27qlogn)

备注:由于POJ编译器太垃圾,矩阵乘法传输数组会CE,所以代码很长。

#include<cstdio>
#include<algorithm>
using namespace std;
const int mod=;
int n,a[][],ret[][],c[][];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<;i++)
for(int j=;j<;j++)
ret[i][j]=(i==j);
a[][]=,a[][]=,a[][]=;
a[][]=,a[][]=,a[][]=;
a[][]=,a[][]=,a[][]=;
while(n)
{
if(n&)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
{
c[i][j]=;
for(int k=;k<;k++)c[i][j]=(c[i][j]+ret[i][k]*a[k][j])%mod;
}
for(int i=;i<;i++)
for(int j=;j<;j++)
ret[i][j]=c[i][j];
}
for(int i=;i<;i++)
for(int j=;j<;j++)
{
c[i][j]=;
for(int k=;k<;k++)c[i][j]=(c[i][j]+a[i][k]*a[k][j])%mod;
}
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=c[i][j];
n>>=;
}
printf("%d\n",ret[][]);
}
}

POJ3734Blocks(递推+矩阵快速幂)的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  5. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

  6. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  9. 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

    Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  10. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

随机推荐

  1. 随机森林(Random Forest)

    阅读目录 1 什么是随机森林? 2 随机森林的特点 3 随机森林的相关基础知识 4 随机森林的生成 5 袋外错误率(oob error) 6 随机森林工作原理解释的一个简单例子 7 随机森林的Pyth ...

  2. linux audit审计(5)--audit规则配置

    audit可以配置规则,这个规则主要是给内核模块下发的,内核audit模块会按照这个规则获取审计信息,发送给auditd来记录日志. 规则类型可分为: 1.控制规则:控制audit系统的规则: 2.文 ...

  3. Front-end Job Interview Questions

    Front-end Job Interview Questions 前端面试 https://github.com/h5bp/Front-end-Developer-Interview-Questio ...

  4. Real-time chart using ASP.NET Core and WebSocket

    Solution in glance The following diagram illustrates our solution where IoT device reports readings ...

  5. ubuntu系统安装mysql(deb-bundle包)

    由于某些原因,又要在ubuntu系统中安装mysql了,之前曾经安装过好多次.都没记下来 以前一直动用源码包来安装,基于两个原因:1.一直用Python写代码.2.想使用文件来安装,而不是通过api ...

  6. 67 个JavaScript和CSS实用工具、库与资源

    在这篇文章中,我不会与大家谈论大型的前端框架,如 React.Angular.Vue 等,也没有涉及那些流行的代码编辑器,如 Atom.VS Code.Sublime,我只想与大家分享一个有助于提升开 ...

  7. 前端 -- HTML内容

    HTML介绍 Wed服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  8. luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

  9. 【NOI2003——搜索+二分图匹配优化】

    A 文本编辑器 无旋treap真好看 B 木棒游戏 暴力神仙题 C 数据生成器 树的直径两端点为Y, Z D 智破连环阵 搜索+二分图匹配优化 第一次写欸 列一下 void dfs (int y,in ...

  10. Android性能优化案例研究

    译 者前言: 这是Google的Android开发工程师Romain Guy刊登在个人Blog上的一篇文章.Romain Guy 作为Android图形渲染和系统优化的专家,是Android 4.1中 ...