题目大意:

  f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d),已给递推公式,求f(n)的大小。

解题思路:

  n很大,所以我们就要构造矩阵,运用矩阵快速幂来求解。//题目描述上口口声声说int范围内,但是大家一定不要天真!!!!!!

 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; #define LL long long
const int maxn = ;
LL d, m;
struct mat
{
LL p[maxn][maxn];
}; mat mul (mat a, mat b);
mat pow (LL n, mat a, mat b); int main ()
{
LL n;
mat a, b; while (scanf ("%lld %lld %lld", &d, &n, &m), n+m+d)
{
memset (a.p, , sizeof(a.p));
memset (b.p, , sizeof(b.p)); for (int i=; i<d; i++)//构造矩阵
{
scanf ("%lld", &a.p[i][]);
a.p[i][] %= m;
a.p[i][i+] = ;
}
for (int i=; i<d; i++)//这个矩阵要反过来输入!!!!!!
{
scanf ("%lld", &b.p[][d-i-]);
b.p[][i] %= m;
} if (d < n)
{
b = pow (n-d, a, b);
printf ("%lld\n", b.p[][]);
}
else
printf ("%lld\n", b.p[][d-n]);
}
return ;
} mat mul (mat a, mat b)
{
mat c;
memset (c.p, , sizeof(c.p));
for (int i=; i<d; i++)
for (int j=; j<d; j++)
{
for (int k=; k<d; k++)
c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j]) % m;
}
return c;
}
mat pow (LL n, mat a, mat b)
{
while (n)
{
if (n % )
b = mul (b, a);
a = mul (a, a);
n /= ;
}
return b;
}

最新文章

  1. python基础七
  2. iosOpenDev-install 失败官方wiki无法解决看这里(尝试有效)
  3. WebStorm 9 自动编译 SCSS 产出 CSS 和 source maps
  4. CLion注册码算法逆向分析实录
  5. 改变Visual Studio 2012的皮肤
  6. 【转】使用BBB的device tree和cape(重新整理版)
  7. Visual Studio发布项目到远程服务器的步骤
  8. jQuery 之 $(this) 出了什么问题?
  9. &lt;Win32_18&gt;平滑的人物走动 —— 解决闪屏
  10. 使用Identity Server 4建立Authorization Server (5)
  11. Linux如此“自私”?
  12. es6 ...克隆与函数深度克隆
  13. stm32高级定时器的应用——spwm
  14. 系统不支持WP开发
  15. linux 权限之acl
  16. 安卓开发_数据存储技术_sqlite
  17. airflow
  18. Python量化库大全
  19. gentoo intel 安装桌面
  20. [leetcode]Distinct Subsequences @ Python

热门文章

  1. 报错: The type ByteInputStream is not accessible due to restriction on required library
  2. 【stl学习笔记】deques
  3. spring mvc 整理
  4. RxJava系列之中的一个 初识Rxjava
  5. hdoj 4790 Just Random 【数学】
  6. Codeforces Round #221 (Div. 2) D
  7. 【Mongodb教程 第七课 】MongoDB 查询文档
  8. asp.net mvc3的静态化实现
  9. Android 使用图片异步载入框架Universal Image Loader的问题
  10. jsp_类的封装_集合的应用