A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155 Accepted Submission(s): 110
 
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
 
/*
题意:给你题目描述的函数,然后让你求f(k)%m的值 初步思路:简单的爆一下 #错误:超内存! #改进思路:
想一下这个式子的实质,不用递归写,用循环写,但是需要循环1e9次,肯定是不可行的,可以用矩阵相乘f(x)是从0,1,2...9乘上a0,a1,a2...
.a9递推过来的这样就能得出来一个矩阵,线性代数还没学,矩阵真的有点麻烦,构造矩阵的时候老是想错了。 #再次错误:矩阵相乘虽然可以解决空间问题,但是没法解决时间问题;这里真的有点糊涂了,常数都有快速幂,矩阵的快速幂怎么就没有想起来呐!
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll m,k;
struct Matrix{
ll m[][];
void init0(){//构造用来乘的矩阵(相当于“幺元”吧,随便想到了离散中的概念就这么叫吧)
memset(m,,sizeof m);
for(int i=;i<;i++)
m[i][i-]=;
}
void init1(){//构造最初的a0~a9
memset(m,,sizeof m);
for(int i=;i<;i++){
m[i][]=-i;
}
}
};
Matrix Mul_Matrix(Matrix a,Matrix b){
Matrix c;
c.init0();
for(int i=;i<;i++){
for(int j=;j<;j++){
c.m[i][j]=;
for(int k=;k<;k++){
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%m;
}
c.m[i][j]%=m;
}
}
return c;
}
/**************矩阵快速幂*********************/
Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul_Matrix(a,b);
}
a=Mul_Matrix(a,a);
x>>=;
}
return b;
}
/**************矩阵快速幂*********************/
Matrix a,b;
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%lld%lld",&k,&m)!=EOF){
a.init0();
b.init1();
//cout<<k<<" "<<m<<endl;
for(int i=;i<;i++) scanf("%lld",&a.m[][i]);
// for(int i=9;i<k;i++){
// b=Mul_Matrix(a,b);
// for(int i=0;i<10;i++){
// for(int j=0;j<10;j++)
// cout<<a.m[i][j]<<" ";
// cout<<" ";
// for(int j=0;j<10;j++)
// cout<<b.m[i][j]<<" ";
// cout<<endl;
// }
// }
Matrix c=Pow(a,b,k-);//利用矩阵快速幂
printf("%lld\n",c.m[][]);
}
return ;
}

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)的更多相关文章

  1. HDU1757 A Simple Math Problem 矩阵快速幂

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

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

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

  3. A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列

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

  4. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  5. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

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

  7. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  8. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x & ...

  9. hdu5015 矩阵快速幂233(好题)

    题意:       给你一个(n+1)*(m+1)的矩阵mat,然后给你mat[0][1] = 233 ,mat[0][2] = 2333,mat[0][3] = 23333...,然后输入mat[1 ...

随机推荐

  1. OC——多态

    书接上文,上文提到继承一个很大用途的是为了更好的实现多态,现在我们就来看看OC的多态. 多态:顾名思义就是好多种状态,以前学C#时候印象最深刻的例子是好多个类共同实现同一个接口,然后把这些类的对象都装 ...

  2. 用vue开发一个app(2,main.js)

    昨天跟着vue的官网搭建了vue的一个脚手架,我也是第一次用VUE一切都在摸索阶段. 今天试着看下里面脚手架里面有点什么东西 先看看main.js 导入了3个模块 一个vue,一个app,还有rout ...

  3. SpringBoot开发案例之mail中文附件乱码

    前一段时间做过一个邮件发送的服务,以前大体都测试过,文本.图片.附件都是没有问题的,可有同事反应发送的附件名称有中文乱码,类似如下截图展示: 咋一看不像乱码,抱着试试看的态度,为MimeMessage ...

  4. EntityFramework Core饥饿加载忽略导航属性问题

    前言 .NET Core项目利用EntityFramework Core作为数据访问层一直在进行中,一直没有过多的去关注背后生成的SQL语句,然后老大捞出日志文件一看,恩,有问题了,所以本文产生了,也 ...

  5. ThinkPHP中:检查Session是否过期

    1.创建Session public function index(){ $sess_time=time(); session('name','andy'); session('time_stamp' ...

  6. 【JVM】Java中的JavaCore/HeapDump文件及其分析方法

    产生时间 Java程序运行时,有时会产生JavaCore及HeapDump文件,它一般发生于Java程序遇到致命问题的情况下. 有时致命问题发生后,Java应用不会死掉,还能继续运行: 但有时致命问题 ...

  7. Spring Boot Document Part II(下)

    Part II. Getting started 11. 开发第一个Spirng Boot Application使用Spring Boot的关键特征开发一个基于JAVA Web的“Hello Wor ...

  8. 使用docker部署standalone cinder

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 背景 OpenSta ...

  9. app启动页问题

    今天自己做的小作品准备提交,就差一个启动页,各种百度,各种搜,结果还好最后终于出来了,和大家分享一下,这个过程中遇到的各种小问题.(注XCode版本为7.2) 1.启动页一般都是图片,因为苹果有4,4 ...

  10. 基于HTML5和WebGL的3D网络拓扑结构图

    现在,3D模型已经用于各种不同的领域.在医疗行业使用它们制作器官的精确模型:电影行业将它们用于活动的人物.物体以及现实电影:视频游戏产业将它们作为计算机与视频游戏中的资源:在科学领域将它们作为化合物的 ...