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

时间:2022-10-13 07:47:20

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

矩阵乘法

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

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

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

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

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

矩阵快速幂见这篇博客

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 &lpar;矩阵快速幂&rpar;

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

  2. HDU 1757 A Simple Math Problem&lpar;矩阵&rpar;

    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 &lpar;乘法矩阵&rpar;

    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. 最全数据结构详述&colon; List VS IEnumerable VS IQueryable VS ICollection VS IDictionary

    本文对常用的数据结构详述:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryabl ...

  2. sleep&lpar;&rpar; 和 wait&lpar;&rpar; 的区别

    好多面经上都出现了,有必要好好熟悉一下 区别: 1.wait() 可以指定时间,也可以不指定(等五分钟你进来,或者是不叫你一直等着):sleep()必须指定时间(不能一睡不起) 2.wait()是Ob ...

  3. 10327 - Flip Sort

    原文链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&pa ...

  4. go的优缺点

    1.1 不允许左花括号另起一行1.2 编译器莫名其妙地给行尾加上分号1.3 极度强调编译速度,不惜放弃本应提供的功能1.4 错误处理机制太原始1.5 垃圾回收器(GC)不完善.有重大缺陷1.6 禁止未 ...

  5. 修改Unity脚本模板的方法合计

    作为一个习惯于偷懒的程序,重复性的无聊内容是最让人无奈的事,就比如我们创建Unity脚本之后,需要手动调整生成的新脚本的格式.编码.内容:如果我们要编写的是编辑器或者服务器端脚本,需要修改的内容就会更 ...

  6. 9&period;27 noip模拟试题

    工资 (money/money.in/money.out) 时限1000ms 内存256MB 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可 ...

  7. Directory类

    string[] pathes = Directory.GetDirectories(@"G:\ReawFiles");//获得所有的文件夹 string[] pathes = D ...

  8. 简易商品信息管理系统——首个Web项目

    正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...

  9. java - day004 - 数组排序&comma;插入&comma;冒泡

    // 判断string 是否相等不能用 == 使用 equal 方法 Arrays.sort(数组);  数组排序算法 对基本类型. 优化的快速排序算法 对引用类型, 优化的合并排序算法

  10. JS模块化开发(三)——seaJs&plus;grunt

    1.seaJs直接构建存在的问题 由于模块之间的依赖require引用的是模块名,当多个js模块被合并成一个时,会由于找不到模块名而报错 2.seaJs+grunt开发 用到的插件:grunt-cmd ...