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. Android 动画的分类

    分为三类: View Animation (补间动画 Tween动画) Drawable Animation(帧动画 Frame动画) Property Animation(android 3.0引入 ...

  2. C基于客户端的通信实例

    运行环境liunx 需求:客户端对服务器发送一个TCP连接,连接成功之后发送数据并接收返回数据 代码如下: #include <sys/socket.h> #include <net ...

  3. Jmeter之基本介绍

    初学Jmeter,以下是我常用的功能,总结一下. 1.Thread Group线程组 线程组,即:虚拟用户组

  4. C#把gird数据导出到excel

    /// <summary>        /// DataTable导出到Excel        /// </summary>        /// <param na ...

  5. 鼠标形状css样式

    鼠标形状css样式 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. Zookeeper,Kafka,Spark关系

    Kafka中ZooKeeper的用途 正如ZooKeeper用于分布式系统的协调和促进,Kafka使用ZooKeeper也是基于相同的原因.ZooKeeper用于管理.协调Kafka代理.每个Kafk ...

  7. 利用python3.5 构建流媒体后台音视频切换的服务端程序

    #!/usr/bin/env python3.5.0 # -*- coding:utf8 -*- import os,sys,socket,hashlib,time,select,threading, ...

  8. 破译情报-NOIP2016提高组复赛模拟试题

    [题目描述] 最近国安人员截获了一份 RB 国的秘密情报, 全文都是经过加密的,每个单 词都很长.破译人员想到先把单词化简一下,方法是把每个单词尽量取短些的前 缀,但所取的前缀不能是其他单词的前缀. ...

  9. PHP静态延迟绑定简单示例

    没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了 <?php class A { protected static $def = '123456'; public stat ...

  10. Mediawiki随笔

    http://www.ibm.com/developerworks/cn/opensource/os-mediawiki/  定制wiki http://www.zzbaike.com/wiki/Me ...