题目地址:HDU 2604 Queuing

题意: 

分析:

易推出:   f(n)=f(n-1)+f(n-3)+f(n-4)

构造一个矩阵:

然后直接上板子:

/*
f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio>
#include<cstring> using namespace std; const int N = 4;
int L, M; struct mtx
{
int x[N+1][N+1];
mtx(){
memset(x, 0, sizeof x );
}
}; mtx operator *(const mtx &a, const mtx &b){
mtx c;
for(int i=0; i<N; ++i)
{
for(int j=0; j<N; ++j)
{
for(int k=0; k<N; ++k)
{
c.x[i][j] = (c.x[i][j] + a.x[i][k]*b.x[k][j]) % M;
}
}
}
return c;
} mtx operator ^(mtx a, int n)
{
mtx res;
for(int i=0; i<N; ++i) res.x[i][i] = 1;
for(; n; n>>=1)
{
if(n&1) res = res * a;
a = a * a;
}
return res;
} int f[N+1];
mtx I; void init()
{
f[1] = 2;
f[2] = 4;
f[3] = 6;
f[4] = 9;
I.x[0][0] = I.x[0][2] = I.x[0][3] = I.x[1][0]
= I.x[2][1] = I.x[3][2] = 1;
} void work()
{
if(L<=4){
printf("%d\n", f[L] % M);
return ;
}
mtx res = I^(L-4);
int F_n = 0;
for(int i=0; i<N; ++i)
{
F_n = (F_n + res.x[0][i]*f[N-i] ) % M;
}
printf("%d\n", F_n);
}
int main()
{
init();
while(~scanf("%d%d", &L, &M))
{
work();
}
return 0;
}

HDU 2604 Queuing,矩阵高速幂的更多相关文章

  1. HDU 2604 Queuing 矩阵高速幂

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

  2. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  3. ZOJ 3690 &amp; HDU 3658 (矩阵高速幂+公式递推)

    ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...

  4. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  5. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

  6. HDU 2256 Problem of Precision(矩阵高速幂)

    题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...

  7. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

  8. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  9. hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...

随机推荐

  1. getField();在TP5里成什么了?

    拆分为value和column了 $comps=db("company")->where(array("areaid"=>$areaid))-> ...

  2. idea新建文件无法识别(二)

    1,出现问题的步骤: 当想新增一个Hello.java文件时候 选错了文件类型.新建了一个text文件.当回过头来把文件后缀改掉的时候发现idea无法识别,颜色一直是灰色. 2,解决问题的办法: 选择 ...

  3. python请求服务器图片并下载到本地磁盘

    >>> import os >>> os.getcwd() 'C:\\Python33' >>> os.chdir('E:\\python\\mm ...

  4. 5.26 Quartz任务调度图解2

  5. macOS下登录store或者xcode等应用时提示【this action could not be completed】

    sudo mkdir -p /Users/Shared sudo chown root:wheel /Users/Shared sudo chmod -R 1777 /Users/Shared === ...

  6. Matlab atan2

    对Matlab不是很熟悉,在这个Matlab atan2 函数上出现了问题. 百度知道上的解释是这样的: atan2() 区别于 atan() 函数,返回 -pi~+pi 范围的角度: 使用过程中发现 ...

  7. An interesting scroll background------ActionScript3.0

    package { /* *@ ClassName : package::backGround *@ INTRO : the continuously scroll background *@ Aut ...

  8. vs2008 打开项目 无法读取项目文件

    卸载vs2015之后 出现问题 C:\Windows\SysWOW64\regedit.exe 64系统运行这个 删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MS ...

  9. eas之日期选择控件

    初始化打印控件KDPrinter ctrlPrinter = new KDPrinter(); 增加列 // 指定插入位置table.addColumn(index);// 插入到最后table.ad ...

  10. C语言右移操作在汇编层面的相关解释

    在 CSDN 看到帖子回复如下: x=y>>2;004122A8  mov         eax,dword ptr [y] 004122AB  sar         eax,2 '算 ...