【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 ...
随机推荐
- 别了 oi——一篇高三狗的滚粗遗言
/* 开始于2015年12月 结束于2016年11月 一年的oi生涯有很多值得怀念的事 还记得去年旺哥找我学oi 当时是一脸的蒙逼 要知道 高二才开始搞是很晚了 然而 也就是那一晚之后 许多事情都变了 ...
- C#Combobox显示名称保存代码
private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add ...
- jBPM开发入门指南
http://blog.csdn.net/eric474470/article/details/7665265 工作流虽然还在不成熟的发展阶段,甚至还没有一个公认的规范标准.但其应用却已经在快速展开, ...
- SQL server 2012 如何取上个月的最后一天
好吧 QQ群里被问到这种问题,还是这里写一下吧. DECLARE @date DATETIME = getdate(); SELECT EOMONTH (@date) AS 'Last Day Of ...
- A题笔记(11)
No.1508 代码:https://code.csdn.net/snippets/192058 考察点:①char型字符转换成对应的 ASKII 的编码 可以通过这样对 string 的每一个字符 ...
- 那天有个小孩跟我说LINQ(一) 转载
1 LINQ准备(代码下载) 新建项目 linq_Ch1控制台程序,新建一个Entity文件夹 1.1 对象初始化器 在Entity新建一个类Student,代码如下 using S ...
- Virtual Studio C++ Version Macro - _MSC_VER
MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC ...
- php session_id()函数介绍及代码实例
session_id()功能: 获取设置当前回话ID. 函数说明: string session_id ([ string $id ] ) 参数: 如果指定了参数$id,那么函数会替换当前的回话id. ...
- windows访问linux共享
1. 安装samba yum install samba 2. 配置samba配置文件,添加共享文件夹 vim /etc/samba/smb.conf 3. 关闭selinux vi /etc ...
- swing容器继承重绘问题解决
swing容器继承重绘问题解决 以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...
, is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.