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

Source

2007省赛集训队练习赛(6)_linle专场

Recommend

lcy

矩阵乘法

如图所示,可以将递推式写成矩阵形式

类似于将斐波那契数列写成矩阵形式

因为数据量很大,需要用到矩阵快速幂

矩阵快速幂见这篇博客

http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html

需要注意的一个小细节是,在进行乘法(不是矩阵乘法)运算时,注意要模上一个数,防止溢出(因为这个WA了好几发)

代码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
int m[10][10];
node(){
memset(m,0,sizeof(m));
// for(int i=0;i<10;i++){
// for(int j=0;j<10;j++)
// m[i][j]=0;
// }
}
};
int mod;
node multi(node &a,node &b)
{
node tmp;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
int sum=0;
for(int k=0;k<10;k++){
sum+=(a.m[i][k]%mod)*(b.m[k][j]%mod);
}
tmp.m[i][j]=sum;
}
}
return tmp;
}
void e_mat(node &a)
{
for(int i=0;i<10;i++){
a.m[i][i]=1;
}
}
node quick_mul(node &a,int n)
{
node tmp=a;
//e_mat(tmp);
node res;
e_mat(res);
if(n&1){
res=a;
}
n=n>>1;
while(n!=0){
tmp=multi(tmp,tmp);
if(n&1){
res=multi(res,tmp);
}
n=n>>1;
}
return res;
}
void print(node &a)
{
cout<<"---------------------------"<<endl;
for(int i=0;i<10;i++){
cout<<i<<"ÐÐ"<<"\t";
for(int j=0;j<10;j++){
cout<<a.m[i][j]<<"\t";
}
cout<<endl;
}
cout<<"---------------------------"<<endl;
}
int main()
{
//freopen("data.in","r",stdin);
int n; int ai[11];
node x;
for(int i=0;i<10;i++){
x.m[i][0]=9-i;
}
while(~scanf("%d%d",&n,&mod)){
for(int i=0;i<10;i++){
scanf("%d",ai+i);
}
if(n<10){
printf("%d\n",n);
continue;
}
node a;
//print(a);
for(int i=0;i<10;i++){
a.m[0][i]=ai[i];
}
//print(a);
for(int i=1;i<10;i++){
a.m[i][i-1]=1;
}
//print(a);
node res=quick_mul(a,n-9);
//print(res);
//print(x);
res=multi(res,x);
//print(res);
printf("%d\n",(res.m[0][0])%mod);
}
}

HDU-1757--A Simple Math Problem(矩阵乘法)的更多相关文章

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

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

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

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

  3. HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...

  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 (矩阵乘法)

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

  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 (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  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 > ...

  10. hdu 1757 A Simple Math Problem (矩阵快速幂)

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

随机推荐

  1. Spark 2.x不支持ALTER TABLE ADD COLUMNS,没关系,我们改进下

    SparkSQL从2.0开始已经不再支持ALTER TABLE table_name ADD COLUMNS (col_name data_type [COMMENT col_comment], .. ...

  2. java中的instanceof

    instanceof是Java.php的一个二元操作符(运算符),和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实 ...

  3. Java实践 — SSH远程执行Shell脚本(转)

    原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介         SSH是Secure Shell的缩写,一 ...

  4. JMS ActiveMQ案例

    创建一个web工程 导入ActiveMQ依赖的jar包  activemq-all-5.9.jar 写一个生产者(send)servlet package com.sun.jms;import jav ...

  5. python自动化开发-2

    1.python的数据类型之列表 列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作. 列表的定义:例子 names = ["Lucy",& ...

  6. 打印java堆栈信息

    使用如下命令: kill -3 {pid} 可以打印指定线程的堆栈信息到tomcat的catalina.out日志中.在性能测试过程中,可以观察响应时间的曲线,如果突然出现波峰则抓取当前时间点tomc ...

  7. WPF和Winform的一些界面控件

    DevExpressTelerikMahApps.MetroModern UI for WPFModernWPFExtended WPF Toolkit™ Community EditionModer ...

  8. cookie会话技术

    会话技术 B/S请求是无状态无记忆的,脚本与脚本之间是没有联系的,导致不能进行连续的业务逻辑 Cookie技术:将会话数据保存在浏览器端 原理:服务器向浏览器发送指令,用来管理存储在浏览器端的cook ...

  9. eclipse 配置 Tomcat 遇到的问题以及解决办法

    Eclipse是一个开发JSP的很好的工具,而笔者在配置Tomcat服务器的时候遇到了一些小问题,在这里给大家总结一些经验,希望能帮助同样遇到这些问题的广大同行们能够简单轻松地解决这些问题~ 笔者在以 ...

  10. 通过linux的iso镜像安装(RPM)扩展工具包

    通过linux的iso镜像安装(RPM)扩展工具包 在linux安装软件时,现在越来越流行通过rpm指令安装完成,原因是:采用RPM安装简单方便:越来越多的软件提供RPM安装包:linux的IOS镜像 ...