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. 玩转docker镜像和镜像构建

    摘要 本文从个人的角度,讲述对于docker镜像和镜像构建的一些实践经验.主要内容包括利用docker hub进行在线编译,下载镜像,dind的实践,对于镜像的一些思考等.本文是对当时微信分享内容的一 ...

  2. Hexo站点之域名配置

    摘要 因为Hexo个人博客是托管在github之上,每次访问都要使用githubname.github.io这么一个长串的域名来访问,会显得非常繁琐.这个时候我们可以购买一个域名,设置DNS跳转,以达 ...

  3. c数据结构学习随笔

    #include <stdio.h> #include <stdlib.h> #include "PublicDS.h" #include<Windo ...

  4. CentOS下使用NVM

    查看CentOS版本 # rpm -q centos-release centos-release-6-8.el6.centos.12.3.x86_64 安装epel源 32位系统选择: rpm -i ...

  5. Mybatis原理图

    Mybatis原理图 MyBatis 是一个基于Java的持久层框架.它提供的持久层框架包括SQL Maps和Data Access Objects(DAO). MyBatis 是支持普通 SQL查询 ...

  6. 51nod1279(二分)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1279 题意:中文题诶- 思路: 就想短板效应一样,很显然决定 ...

  7. Mysql之Windows初探

    准备工作 防止原先mysql残留,DOS模式下删除mysql服务 sc delete mysql 或者 进入mysql目录下子目录bin卸载mysql服务 mysqld --remove mysql ...

  8. Ubuntu 14.04—Anaconda 相关

    Anaconda 使用国内镜像: https://keyunluo.github.io/2016/07/17/2016-07-17-anaconda-python.html Anaconda下的 De ...

  9. JavaScript简单的一些....

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Markdown引用本地图片语法

    Markdown引用本地图片语法 markdown引用图片标准方式如下: ![Alt text](/path/to/img.jpg) 测试markdown文本如下: # 测试相对路径图片 ![Alt ...