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. 在JavaScript中使用json.js:使得js数组转为JSON编码

    在json的官网中下载json.js,然后在script中引入,以使用json.js提供的两个关键方法. 1.数组对象.toJSONString() 这个方法将返回一个JSON编码格式的字符串,用来表 ...

  2. HDFS 分布式写入问题 AlreadyBeingCreatedException

    进行追加文件时出现AlreadyBeingCreatedException错误 堆栈信息大致如下: org.apache.hadoop.ipc.RemoteException(org.apache.h ...

  3. JSP入门2

    1. CRUD是Create(创建).Read(读取).Update(更新)和Delete(删除)的缩写,一般应用有这四项也就足够了. 我们这里的例子是对联系人信息进行CRUD操作. 2. javab ...

  4. Spring Boot Document Part II(上)

    Part II. Getting started 这一章内容适合刚接触Spring Boot或者"Spring"家族的初学者!随着安装指导说明,你会发现对Spring boot有一 ...

  5. E. Fish (概率DP)

    E. Fish time limit per test 3 seconds memory limit per test 128 megabytes input standard input outpu ...

  6. bzoj4403(模板题)

    序列统计,将答案转化,然后就是Lucas的模板题,用费马小定理瞎搞. #include<cstdio> #include<iostream> #include<algor ...

  7. 集群提交spark任务命令

    >>spark-submit --class WordCount  DataMining.jar /dept_ana/part-00000 /dept_ana/output/wordCou ...

  8. Linux下安装与配置Nginx

    一.准备 Nginx版本:nginx-1.7.7.tar.gz   请自行到官网下载对应的版本. 二.步骤 ♦在Linux新建一个queenLove用户 [root@localhost /]# use ...

  9. C#中 计时器用法

    有时候我们会需要计算某段代码运行的时间比如一个sql查询,记录一段代码所花费的时间等等代码如下: System.Diagnostics.Stopwatch watch = new System.Dia ...

  10. zoj1002 Fire Net

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...