【题目链接】

http://acm.hdu.edu.cn/showproblem.php?pid=5015

【算法】

矩阵乘法

【代码】

#include<bits/stdc++.h>
using namespace std;
const int P = ; int i,j,n,m,ans;
int a[],val[]; struct Matrix
{
int mat[][];
} b; inline void multipy(Matrix &a,Matrix b)
{
int i,j,k;
Matrix res;
memset(res.mat,,sizeof(res.mat));
for (i = ; i <= n + ; i++)
{
for (j = ; j <= n + ; j++)
{
for (k = ; k <= n + ; k++)
{
res.mat[i][j] = (res.mat[i][j] + 1ll * a.mat[i][k] * b.mat[k][j]) % P;
}
}
}
a = res;
}
inline Matrix power(Matrix a,int n)
{
Matrix res;
memcpy(res.mat,a.mat,sizeof(a.mat));
n--;
while (n > )
{
if (n & ) multipy(res,a);
multipy(a,a);
n >>= ;
}
return res;
} int main()
{ while (scanf("%d%d",&n,&m) != EOF)
{
ans = ;
a[] = ;
for (i = ; i <= n; i++) scanf("%d",&a[i]);
val[] = ;
for (i = ; i <= n; i++) val[i] = (val[i-] % P + a[i] % P) % P;
val[n+] = ;
if (m == )
{
printf("%d\n",val[n]);
continue;
}
memset(b.mat,,sizeof(b.mat));
for (i = ; i <= n; i++)
{
b.mat[i][] = ;
b.mat[i][n+] = ;
for (j = ; j <= n; j++)
{
b.mat[i][j] = (i >= j);
}
}
b.mat[n+][n+] = ;
b = power(b,m-);
for (i = ; i <= n + ; i++) ans = (ans + 1ll * b.mat[n][i] * val[i]) % P;
printf("%d\n",ans); } return ; }

【HDU 5015】233 Matrix的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【HDU 2157】 How Many Ways??

    [题目链接] 点击打开链接 [算法] 设A[i][j]为走一条边,从i走到j的方案数 C[i][j]为走两条边,从i走到j的方案数,显然有 : C = A * A = A^2 C'[i][j]为走三条 ...

  8. 【HDU 1005】 Number Sequence

    [题目链接] 点击打开链接 [算法] 矩阵乘法快速幂,即可 [代码] #include<bits/stdc++.h> using namespace std; int a,b,n; str ...

  9. 【HDU 1588】 Gauss Fibonacci

    [题目链接] 点击打开链接 [算法] 要求 f(g(0)) + f(g(1)) + f(g(2)) + ... + f(g(n-1)) 因为g(i) = k * i + b 所以原式 = f(b) + ...

随机推荐

  1. O - Masha and Bears

    Problem description A family consisting of father bear, mother bear and son bear owns three cars. Fa ...

  2. B - Even Odds

    Problem description Being a nonconformist, Volodya is displeased with the current state of things, p ...

  3. MD5三种方法的学习总结

    MD5百度百科 MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已 ...

  4. lua中.和:的区别

    local myTable = {} function myTable:putMyname(val) print(val) print(self and self.name) end myTable. ...

  5. 第5章分布式系统模式 Data Transfer Object(数据传输对象)

    正在设计一个分布式应用程序,为了满足单个客户端请求,您发现自己对一个远程接口发出了多个调用,而这些调用所增加的响应时间超出了可接受的程度. 影响因素 在与远程对象通信时,请考虑下列需要权衡的因素: 远 ...

  6. Tomcat 报错 记录

    Resource is out of sync with the file system: 该错误为替换了image中的图片而没有进行更新,造成找不到该资源,进而保存,解决只要eclipse刷新一下F ...

  7. 【OpenCV】关于 waitKey()的使用方法

    C++: int waitKey(int delay=0) cvWaitKey()函数的功能是不断刷新图像,频率时间为delay,单位为ms. 返回值为当前键盘按键值. 所以显示图像时,如果需要在cv ...

  8. 整理Py小demo

    from email.mime.text import MIMEText # 第一个参数就是邮件正文,第二个参数是MIME的subtype, # 传入'plain'表示纯文本,最终的MIME就是'te ...

  9. aes加密算法的一个问题

    百度“delphi aes”出来的结果,千篇一律,都是相互转载,但是没有人发现EldoS, Alexander Ionov于1998-2001写的ElAes.pas代码,存在一个问题. 相同的key和 ...

  10. C51端口结构和工作原理(转)

    一.P0端口的结构及工作原理 P0端口8位中的一位结构图见下图:   由上图可见,P0端口由锁存器.输入缓冲器.切换开关.一个与非门.一个与门及场效应管驱动电路构成.再看图的右边,标号为P0.X引脚的 ...