【HDU 1757】 A Simple Math Problem
Description
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
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
Sample Input
Sample Output
题意:按f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10) (x>=10) ; f(x) = x(x<10)来计算f(x)%m的值。
分析:这题要用递推,并且k值很大,所以需要用矩阵快速幂。
构造的矩阵是:
|
* |
|
= |
|
写个结构类型代表矩阵,以及矩阵的相乘的函数和矩阵快速幂的函数,注意一下初始化。
#include<stdio.h>
#include<string.h>
int n,k,m;
struct matrix
{
int a[][];
int row,col;
void init(int r,int c){
memset(a,,sizeof(a));
row=r;col=c;
}
} big,f,u;
matrix mul(matrix a,matrix b)
{
matrix c;
c.init(a.row,b.col);
for(int i=; i<a.row; i++)
for(int j=; j<b.col; j++)
{
for(int k=; k<a.col; k++)
c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m;
c.a[i][j]%=m;
}
return c;
}
void init()
{
big.init(,);
f.init(,);
for(int i=; i<; i++)
big.a[i][i-]=;
for(int i=; i<; i++)
f.a[i][]=-i;
}
matrix qpow(matrix a,int k)
{
matrix ans;
ans.init(a.row,a.col);
for(int i=;i<a.row;i++)
ans.a[i][i]=;
while(k)
{
if(k&)ans=mul(ans,a);
a=mul(a,a);
k>>=;
}
return ans;
}
int main()
{
init();
while(~scanf("%d%d",&k,&m))
{
for(int i=; i<; i++)
scanf("%d",&big.a[][i]);
u=mul(qpow(big,k-),f);
printf("%d\n",u.a[][]%m);
}
return ;
}
【HDU 1757】 A Simple Math Problem的更多相关文章
- 【hdu 2486】A simple stone game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【HDOJ5974】A Simple Math Problem(构造,解方程)
题意:给定A与B,要求构造出一组X,Y,使得X+Y=A,lcm(X,Y)=B A<=2e4,B<=1e9 思路:A的范围较小,考虑以A为突破口 枚举A的约数k,复杂度O(sqrt(A)) ...
- 【HDU 5399】Too Simple
题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...
- 【hdu 5628】Clarke and math (Dirichlet卷积)
hdu 5628 Clarke and math 题意 Given f(i),1≤i≤n, calculate \(\displaystyle g(i) = \sum_{i_1 \mid i} \su ...
- 【bzoj 3489】A simple rmq problem
题目 \(kdt\)就是数点神器 我们先扫两遍处理出每个数上一次出现的位置\(pre_i,nxt_i\),之后变成\((i,pre_i,nxt_i)\)这样一个三维空间上的点 就变成了求一个立方体的最 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- 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 ...
- hdu 1757 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 ...
随机推荐
- Linux命令学习-date
date命令可以用来显示和修改系统日期时间,注意不是time命令. 1.在命令行输入date显示当前时间 [root@vm4 logs]# dateSat Nov 22 00:00:02 CST 20 ...
- DevExpress主从表 按组分页一组不足一页为一页--以此记录
本文的主要是说明Dev的报表的主从表,主从表的每一组显示在一页,当一组超出一页,第二页只显示第一组的. 一.每上报表设置图 简单设计图如上 二.后台代码 报表页代码 public partial cl ...
- 普通请求和ajax请求的区别
普通请求和ajax请求的区别? 下面的action返回一个json文件,文件内容为sts.*,data1
- AVR/Arduino定时/计数器、中断入门
在Arduino中,可以使用AnalogWrite来使用硬件产生490Hz/980Hz的pwm波,并可根据参数来设定占空比.不了解这个的同学可以去AnalogWrite学习下,SecretsOfArd ...
- Linux非root用户安装jdk和tomcat
转载自:http://blog.csdn.net/wuyigong111/article/details/17410661,进行部分修改 创建一个用户 sgmm,并在其用户目录里面安装 jdk和tom ...
- Java 征途:行者的地图 (转)
http://www.cnblogs.com/mindwind/p/5251430.html Java 征途:行者的地图 前段时间应因缘梳理了下自己的 Java 知识体系, 成文一篇望能帮到即将走 ...
- Nutch搜索引擎(第3期)_ Nutch简单应用
1.Nutch命令详解 Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令. 要看Nutch的命令说明,可执行"Nutch&qu ...
- java从0开始学——数组,一维和多维
#,在java中,允许数组的长度为0:也就是允许 int[] zeroLenthArray = new int[0]; #,匿名的数组初始化是合法的: int[] smallPrim ...
- [转载]Java应用程序中的内存泄漏及内存管理
近期发现测试的项目中有JAVA内存泄露的现象.虽然JAVA有垃圾回收的机制,但是如果不及时释放引用就会发生内存泄露现象.在实际工作中我们使用Jprofiler调用java自带的 jmap来做检测还是很 ...
- Python积木之with
简而言之,with 语句是典型的程序块 “try catch finally”的一种模式抽取.python的作者在PEP343中写道 “ This PEP adds a new statement & ...