【HDU 2855】 Fibonacci Check-up (矩阵乘法)
Fibonacci Check-up
Problem DescriptionEvery ALPC has his own alpc-number just like alpc12, alpc55, alpc62 etc.
As more and more fresh man join us. How to number them? And how to avoid their alpc-number conflicted?
Of course, we can number them one by one, but that’s too bored! So ALPCs use another method called Fibonacci Check-up in spite of collision.First you should multiply all digit of your studying number to get a number n (maybe huge).
Then use Fibonacci Check-up!
Fibonacci sequence is well-known to everyone. People define Fibonacci sequence as follows: F(0) = 0, F(1) = 1. F(n) = F(n-1) + F(n-2), n>=2. It’s easy for us to calculate F(n) mod m.
But in this method we make the problem has more challenge. We calculate the formula, is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.
InputFirst line is the testcase T.
Following T lines, each line is two integers n, m ( 0<= n <= 10^9, 1 <= m <= 30000 )OutputOutput the alpc-number.Sample Input2
1 30000
2 30000Sample Output1
3
【题意】
求S(n)=∑C[k][n]*Fibonacci(k) mod m(0<=k<=n)
( 0<= n <= 10^9, 1 <= m <= 30000 )
【分析】
组合数和斐波那契数列都是很有特点的东西,然而我想了一会儿还是没有想出来。
现在又懂得了一点,能写出递推式,像斐波那契数列一样的,它的第k项其实可以表示成矩阵的幂,即A^k,把它当成数一样考虑就很方便。
对于组合数,二项式定理啊真是太厉害了。。终于有点懂母函数的思想啊....

图片转自:http://blog.csdn.net/hzh_0000/article/details/38171903
其实还有第二种方法,我没打,感觉我不太可能推出来。。
第一种方法代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std; struct node
{
int a[][];
}t[]; int n,m; void init()
{
t[].a[][]=;t[].a[][]=;
t[].a[][]=;t[].a[][]=;
} void mul(int x,int y,int z)
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
t[].a[i][j]=;
for(int k=;k<=;k++)
t[].a[i][j]=(t[].a[i][j]+t[y].a[i][k]*t[z].a[k][j])%m;
}
t[x]=t[];
} void get_un()
{
memset(t[].a,,sizeof(t[].a));
for(int i=;i<=;i++) t[].a[i][i]=;
} void qpow(int b)
{
get_un();
while(b)
{
if(b&) mul(,,);
mul(,,);
b>>=;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
qpow(n);
printf("%d\n",t[].a[][]);
}
return ;
}
[HDU 2855]
2016-09-28 14:10:22
【HDU 2855】 Fibonacci Check-up (矩阵乘法)的更多相关文章
- HDU 5607 graph(DP+矩阵乘法)
[题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...
- HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)
题解见X姐的论文 矩阵乘法递推的优化.仅仅是mark一下. .
- HDU 2865 Birthday Toy [Polya 矩阵乘法]
传送门 题意: 相邻珠子不能相同,旋转等价.$n$个珠子$k$中颜色,求方案数 首先中间珠子$k$种选择,$k--$如果没有相邻不同的限制,就和$POJ\ 2154$一样了$|C(f)|=k^{\#( ...
- 【wikioi】1250 Fibonacci数列(矩阵乘法)
http://wikioi.com/problem/1250/ 我就不说这题有多水了. 0 1 1 1 矩阵快速幂 #include <cstdio> #include <cstri ...
- HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )
链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...
- 2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)
题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据 ...
- hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
- HDU 6155 Subsequence Count(矩阵乘法+线段树+基础DP)
题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...
- HDU - 6185 :Covering(矩阵乘法&状态压缩)
Bob's school has a big playground, boys and girls always play games here after school. To protect bo ...
随机推荐
- order by跟group by 跟having(2)
- hibernate篇章三-- hibernate配置文件hibernate.cfg.xml的详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...
- Installation error INSTALL_FAILED_VERSION_DOWNGRADE错误
最近折腾了一下Robotium自动化测试框架,发现问题还挺多,刚刚解决了一个问题,总算是把环境搞定了,可是一运行测试用例,发现又报Installation error INSTALL_FAILED_V ...
- JAVA深入研究——Method的Invoke方法
http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用 ...
- 自动构建工具Ant的使用-笔记
第一:什么是Ant? Apache Ant是一个基于Java的生成工具.据最初的创始人James Duncan Davidson的介绍,这个工具的名称是another neat tool(另一个整洁的 ...
- UITextField文本字段控件的位置
如果需要更改默认的UITextField清除按钮.左右视图等等控件的位置,可以通过如下设置: // 控制清除按钮的位置 (默认 width = 15 = height) -(CGRect)clearB ...
- HDU 2502 月之数(简单递推)
月之数 Problem Description 当寒月还在读大一的时候,他在一本武林秘籍中(据后来考证,估计是计算机基础,狂汗-ing),发现了神奇的二进制数.如果一个正整数m表示成二进制,它的位数为 ...
- Codevs 1191 数轴染色
1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...
- 【转】Java编程之字符集问题研究
发现这是对字集说得最明了的一篇文章了. 转发自:http://tomcat-oracle.iteye.com/blog/2037160 1. 概述 本文主要包括以下几个方面:编码基本知识,java,系 ...
- siege 压力测试
Ben: $ siege -u shemp.whoohoo.com/Admin.jsp -d1 -r10 -c25 ..Siege 2.65 2006/05/11 23:42:16 ..Prepari ...
, is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.