任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6621    Accepted Submission(s): 4071

Problem Description
Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

 
Input
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
 
Output
For each case, output f(k) % m in one line.
 
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
 
Sample Output
45
104
 
Author
linle
 

题意概括:

按照题目所给的递推式求解 f(N);

解题思路:

根据递推式构造矩阵乘法;

然后矩阵快速幂解决矩阵乘法;

Ac code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const int N = ;
int Mod, K; struct mat
{
int m[][];
}base, tmp, ans; mat muti(mat a, mat b)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++){
if(a.m[i][j]){
for(int k = ; k <= N; k++){
res.m[i][k] = (res.m[i][k] + a.m[i][j] * b.m[j][k])%Mod;
}
}
}
return res;
} mat qpow(mat a, int n)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= N; i++) res.m[i][i] = 1LL;
while(n){
if(n&){
res = muti(res, a);
}
a = muti(a, a);
n>>=;
}
return res;
} int main()
{ while(~scanf("%d%d", &K, &Mod)){
memset(base.m, , sizeof(base.m));
for(int i = ; i < ; i++){
base.m[i][] = i;
} memset(tmp.m, , sizeof(tmp.m));
for(int i = ; i <= ; i++){
tmp.m[i][i+] = ;
}
for(int i = ; i >= ; i--){
scanf("%d", &tmp.m[][i]); //构造递推关系矩阵
base.m[][] += ((LL)(i-)*tmp.m[][i])%Mod;
}
//see see
// for(int i = 1; i <= 10; i++){
// for(int j = 1; j <= 10; j++){
// printf("%d ", tmp.m[i][j]);
// }
// puts("");
// } if(K <= ){
printf("%d\n", base.m[K][]);
}
else{
tmp = qpow(tmp, K-);
ans = muti(tmp, base);
// //see see
// for(int i = 1; i <= 10; i++){
// for(int j = 1; j <= 10; j++){
// printf("%d ", tmp.m[i][j]);
// }
// puts("");
// }
printf("%d\n", ans.m[][]%Mod);
}
}
return ;
}

HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】的更多相关文章

  1. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  2. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  4. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

    题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...

  6. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  7. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  8. hdu - 1757 - A Simple Math Problem

    题意:当x < 10时, f(x) = x: 当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) +  + a2 * f(x-3) + …… + a9 ...

  9. hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...

随机推荐

  1. windows下dubbo-admin的安装

    本来以为十分钟就能搞定的东西结果搞了一个小时,也是菜到抠脚,赶紧记录一下. 下载dubbo源码,下载地址:https://download.csdn.net/download/huangzhang_/ ...

  2. TOJ 4008 The Leaf Eaters(容斥定理)

    Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...

  3. 移除“xmlns”命名空间

    用XmlDocument创建一个文档,或者插入一个节点,默认会生成xmlns(命名空间)特性. 假定有一个xml文档如下结构: <?xml version="1.0" enc ...

  4. android 学习资源网址

    脚本之家: http://www.jb51.net/list/list_233_2.htm csdn: http://blog.csdn.net/xubo578/article/details/571 ...

  5. React.js 小书 Lesson10 - 组件的 state 和 setState

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson10 转载请注明出处,保留原文链接和作者信息. state 我们前面提到过,一个组件的显示形态 ...

  6. C# 面试题二

    1.        请编程实现一个冒泡排序算法? int [] array = new int [*] ; ; ; i < array.Length - ; i++) { ; j < ar ...

  7. 不使用XMLHttpRequest实现异步加载:Iframe和script

    运用Iframe和script可以实现简单的异步加载: 调用页面如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

  8. reload()与reload(true)

    如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已 改变.如果文档已改变,reload() 会再次下载该文档.如果文 ...

  9. python发送邮件(带附件)

    python通过stmp发送qq邮件,带附件 import smtplib from email.mime.multipart import MIMEMultipart from email.mime ...

  10. android 短期计划

    http://www.jianshu.com/p/2a9fcf3c11e4 http://www.jianshu.com/p/5f6d79323923 activity启动模式: http://www ...