HDU 2604 Queuing (递推+矩阵快速幂)

这位作者讲的不错,可以看看他的

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std; const int N = 5;
int msize, Mod; struct Mat
{
int mat[N][N];
}; Mat operator *(Mat a, Mat b)
{
Mat c;
memset(c.mat, 0, sizeof(c.mat));
for(int k = 0; k < msize; ++k)
for(int i = 0; i < msize; ++i)
if(a.mat[i][k])
for(int j = 0; j < msize; ++j)
if(b.mat[k][j])
c.mat[i][j] = (c.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod;
return c;
} Mat operator ^(Mat a, int k)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int i = 0; i < msize; ++i)
c.mat[i][i]=1;
for(; k; k >>= 1)
{
if(k&1) c = c*a;
a = a*a;
}
return c;
} int main()
{
// freopen("in.txt","r",stdin);
int n;
msize = 4;
int f[] = {9, 6, 4, 2};
while(~scanf("%d%d", &n, &Mod))
{
if(n <= 4)
{
printf("%d\n", f[4-n] % Mod);
continue;
}
Mat A;
memset(A.mat,0,sizeof(A.mat));
for(int i = 0; i < msize; i++)
A.mat[0][i] = 1;
A.mat[0][1] = 0;
for(int i = 1; i < msize; i++)
A.mat[i][i-1] = 1;
A = A^(n - msize);
int ans = 0;
for(int i=0; i < msize; i++)
ans = (ans + A.mat[0][i]*f[i]) % Mod;
printf("%d\n", ans);
}
return 0;
}

hdu 2604 递推 矩阵快速幂的更多相关文章

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

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

  2. 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] 类似于斐波那契数列的递推式子吧, 但 ...

  3. hdu 6185 递推+矩阵快速幂

    思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下)  假设 ...

  4. HDU Queuing(递推+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

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

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

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

  7. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

  8. hdu 2604 Queuing(矩阵快速幂乘法)

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

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

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

随机推荐

  1. linux中级之lvs概念

    一.lvs介绍 LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器.它是我们国家的章文嵩博士的一个开源项目.在linux内存2.6中,它已经成为内核的一部分,在此之前的 ...

  2. sizeof 的妙用

    (1)sizeof也可以对一个函数调用求值,其结果是函数返回类型的大小,函数并不会被调用.

  3. IDEA中配置maven 全解析教程(Day_08)

    每一个你讨厌的现在,都有一个不够努力的曾经. 一.选择一个maven的版本下载 本文中 maven 下载链接:(apache-maven-3.5.2.rar) https://files-cdn.cn ...

  4. 西门子 S7200 以太网模块连接力控组态方法

    产品简介:北京华科远创科技有限研发的远创智控ETH-YC模块,以太网通讯模块型号有MPI-ETH-YC01和PPI-ETH-YC01,适用于西门子S7-200/S7-300/S7-400.SMART ...

  5. AtCoder Regular Contest 121 D - 1 or 2

    题目链接:点我点我 Problem Statement Snuke has a blackboard and NN candies. The tastiness of the ii-th candy ...

  6. PyTorch 自动微分示例

    PyTorch 自动微分示例 autograd 包是 PyTorch 中所有神经网络的核心.首先简要地介绍,然后训练第一个神经网络.autograd 软件包为 Tensors 上的所有算子提供自动微分 ...

  7. 错误:软件包:php-fpm-5.4.16-42.el7.x86_64 需要:php-common(x86-64)

    报错信息:错误:软件包:php-fpm-5.4.16-42.el7.x86_64 (/php-fpm-5.4.16-42.el7.x86_64)需要:php-common(x86-64) = 5.4. ...

  8. Nginx 配置文件介绍

    目录 1.1 常用命令 1.2 Nginx的配置文件结构 1.3 Nginx的全局配置 1.4 HTTP服务器配置 1.5 HttpGzip配置 1.6 负载均衡配置 1.7 server虚拟主机配置 ...

  9. 【NX二次开发】Block UI 对象颜色选择器

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  10. UF_DRAW 制图操作

    Open C uc6476uc6477uc6478uc6479uc6480uc6481uc6482uc6483uc6484uc6485uc6486uc6488uc6489uc6492uc6494uc6 ...