任意门: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. Oracle RAC集群搭建(二)-基础环境配置

    01,创建用户,用户组 [root@rac1 ~]# groupadd -g 501 oinstall [root@rac1 ~]# groupadd -g 502 dba [root@rac1 ~] ...

  2. (转)IPC相关的命令

    IPC相关的命令 原文:http://www.cnblogs.com/jjzd/p/6773090.html 进程间通信概述 进程间通信有如下的目的: 1.数据传输,一个进程需要将它的数据发送给另一个 ...

  3. docker~环境变量到底怎么用

    docker已经用了两年多了,从开始的简单应用到现在的自动化部署,已经越来越感觉到它的威力,今天把Hitchhiker部署完成后,看到了它与.net core项目有个类似的地方,就是对于多环境部署的时 ...

  4. vsftpd配置文件解析

    对vsftpd配置文件详细解答. 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为f ...

  5. File upload error - unable to create a temporary file

    php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php ...

  6. HAProxy advanced Redis health check---ref

    http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...

  7. JS的从理解对象到创建对象

    JavaScript不是一门真正的面向对象语言,因为它连最基本的类的概念都没有,因此它的对象和基于类的语言中的对象也会有所不同.ECMA-262把对象定义为:“无序属性的集合,其属性可以包含基本值.对 ...

  8. 跨页面传值之QueryString

    跨页面传值常用方法 1.QueryString 2.Form-post控件传递 3.Cookies传递 4.Application传递 5.Session传递(灵活强大) 1.query传值 http ...

  9. 关于EF执行返回表的存储过程

    1.关于EF执行返回表的存储过程 不知道为什么EF生成的存储过程方法会报错,以下方法可以使用,call是MySQL执行存储过程的命令 [HttpGet] public HttpResponseMess ...

  10. My eclipse jdk unbound的解决

    project --> properties --> java build path --> 双击出错的jdk --> alternate jre --> install ...