A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
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. 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 |
|
Sample Output
45 |
|
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(矩阵快速幂)(寒假闭关第一题,有点曲折啊)的更多相关文章
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- 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 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- 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 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- 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 > ...
- 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 & ...
- hdu5015 矩阵快速幂233(好题)
题意: 给你一个(n+1)*(m+1)的矩阵mat,然后给你mat[0][1] = 233 ,mat[0][2] = 2333,mat[0][3] = 23333...,然后输入mat[1 ...
随机推荐
- 程序员的自我修养九Windows下的动态链接
9.1 DLL简介 DLL即动态链接库的缩写,它相对于Linux下的共享对象. Windows下的DLL文件和EXE文件实际上是一个概念,它们都是有PE格式的二进制文件. 微软希望通过DLL机制加强软 ...
- css控制div强制换行
div{white-space:nowrap;} 自动换行 div{ word-wrap: break-word; word-break: normal; } 强制英文单词断行 div{word-br ...
- InnoDB Undo Log
简介 Undo Log包含了一系列在一个单独的事务中会产生的所有Undo Log记录.每一个Undo Log记录包含了如何undo事务对某一行修改的必要信息.InnoDB使用Undo Log来进行事务 ...
- 【转】Spark运行过程
http://www.cnblogs.com/1130136248wlxk/articles/6289717.html
- ObjectSNMP
下面的例子,就是使用ObjectSNMP获取RFC1213-MIB的例子:其中的system和ifTable对象就是对应的SNMPMIB中的system组合interface中的ifTable表. p ...
- 洗礼灵魂,修炼python(4)--从简单案列中揭示常用内置函数以及数据类型
上一篇说到print语句,print是可以打印任何类型到屏幕上,都有哪些类型呢? 整形(int) 长整型(long) 浮点型(float) 字符型(str) 布尔型(bool) 最常见的就这几种. 在 ...
- 征服 Redis
大约一年多前,公司同事开始使用Redis,不清楚是配置,还是版本的问题,当时的Redis经常在使用一段时间后,连接爆满且不释放.印象中,Redis 2.4.8以下的版本由于设计上的主从库同步问题,就会 ...
- selenium webdriver 启动三大浏览器Firefox,Chrome,IE
selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...
- php soap实现WebService接口
nusoap是php写的一个功能文件,下载地址:http://pan.baidu.com/s/1i3mUQJr 一.不使用wsdl服务端 server.php <?php //包函nusoap. ...
- 悟透JavaScript (一)
首先说明,这是别人写的一篇文章,写得很好,对理解JavaScript很有好处,所以转帖过来. 引子 编程世界里只存在两种基本元素,一个是数据,一个是代码.编程世界就是在数据和代码千丝万缕的纠缠中 ...