【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 ...
随机推荐
- 11.14 noip模拟试题
题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer ...
- Magento Block设计分析(深入分析)
Magento中Block是一个很重要的组件,它在Block中充当非常重要的角色,下面我们来分析一下Magento中Block是怎样设计的,我们应该怎样使用这个重要的角色. 1.Magento Blo ...
- Integer跟int的区别(备份回忆)
int与Integer的区别 int 是基本数据类型Integer是其包装类,注意是一个类.为什么要提供包装类呢???一是为了在各种类型间转化,通过各种方法的调用.否则 你无法直接通过变量转化.比如, ...
- A swift Tour
传统的认为,一个新的语言的第一个应用程序都会打印"Hellow,Word",在Swift中,可以只需要一行代码: pringln("Hello, word") ...
- C#高级
程序集 程序集概念: 程序集是.net中的概念. .net中的dll与exe文件都是程序集.(exe与dll的区别(exe有程序主入口,可以执行,dll没有主入口,不可运行)) 程序集(Assembl ...
- 20151209jquery学习笔记Ajax 代码备份
/*$(function () { $("input").click(function() { $.ajax({ type:'POST', url:'test.php', data ...
- SqlServer2008误操作数据(delete或者update)后恢复数据
实际工作中,有时会直接在数据库中操作数据,比如对数据进行delete或者update操作,当进行这些操作的时候,如果没有加上where条件或者where条件不合理,那么导致的结果可想而知,如果操作的又 ...
- IOS 本地通知
操作流程 1.接收通知 2.注册发送通知 用途:提示时间,闹钟 //接收本地通知(在Appdelegate里面实现) - (void)application:(UIApplication *)appl ...
- c#对象初始化
class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...
- IntelliJ IDEA14 和 Maven 系列:使用IntelliJ IDEA 14和Maven 7 创建java web项目(一)
Intellij IDEA作为最好的Java IDE,创建Maven项目还是比较简单的,但是创建一个Maven Web项目还是要修改一些配置的,下面进行总结整理. 1前言 在创建项目中,IDEA提供了 ...
, is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.