Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >1250 Fibonacci数列

1250 Fibonacci数列

作者头像
attack
发布于 2018-04-12 03:28:59
发布于 2018-04-12 03:28:59
71600
代码可运行
举报
运行总次数:0
代码可运行

时间限制: 1 s

 空间限制: 128000 KB

 题目等级 : 钻石 Diamond

题解

 查看运行结果

题目描述 Description

定义:f0=f1=1, fn=fn-1+fn-2(n>=2)。{fi}称为Fibonacci数列。

输入n,求fn mod q。其中1<=q<=30000。

输入描述 Input Description

第一行一个数T(1<=T<=10000)。

以下T行,每行两个数,n,q(n<=109, 1<=q<=30000)

输出描述 Output Description

文件包含T行,每行对应一个答案。

样例输入 Sample Input

3

6 2

7 3

7 11

样例输出 Sample Output

1

0

10

数据范围及提示 Data Size & Hint

1<=T<=10000

n<=109, 1<=q<=30000

分类标签 Tags 点此展开 

最简单的矩阵快速幂优化DP,

退出斐波那契的矩阵然后跑矩阵快速幂就好,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 void read(int &n)
 8 {
 9     char c='+';int x=0;bool flag=0;
10     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
11     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
12     flag==1?n=-x:n=x;
13 }
14 void Matrix_mul(int a[2][2],int b[2][2],int mod)
15 {
16     int c[2][2];
17     memset(c,0,sizeof(c));
18     for(int i=0;i<2;i++)
19         for(int j=0;j<2;j++)
20             for(int k=0;k<2;k++)
21                 c[i][j]=(c[i][j]+(a[i][k]*b[k][j])%mod)%mod;
22     for(int i=0;i<2;i++)
23         for(int j=0;j<2;j++)
24             a[i][j]=c[i][j];
25 }
26 int Matrix_fastpow(int n,int q)
27 {
28     int a[2][2]={1,1,1,0};
29     int ans[2][2]={1,0,1,0};
30     while(n)
31     {
32         if(n&1)
33             Matrix_mul(ans,a,q);
34         Matrix_mul(a,a,q);
35         n>>=1;
36     }
37     //cout<<ans[0][1]<<endl;
38     return ans[0][1];
39 }
40 int main()
41 {
42     int T,n,q;
43     read(T);
44     while(T--)
45     {
46         read(n);read(q);
47         n++;
48         printf("%d\n",Matrix_fastpow(n,q));
49     }
50     return 0;
51 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDU 1575 Tr A(矩阵快速幂)
Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求 Input 数据的第一行是一个T,表示有T组数据。 每组数据的第一行有 两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。 Output 对应每组数据,输出 Sample Input 2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9 Sample Output 2 2686 Author xhd Source
attack
2018/04/12
2.2K0
数论-快速幂、矩阵快速幂、慢速乘
慢速乘,顾名思义,之所以慢是因为把乘法拆成了若干次加法运算,但是我们可以在每次加法时对中间结果进行取模,所以可以防止大数相乘溢出,其原理同快速幂,不再赘述。
唔仄lo咚锵
2022/05/08
3930
数论-快速幂、矩阵快速幂、慢速乘
洛谷P3746 [六省联考2017]组合数问题
题目描述 组合数 C_n^mCnm​ 表示的是从 n 个互不相同的物品中选出 m 个物品的方案数。举个例子,从 (1;2;3) 三个物品中选择两个物品可以有 (1;2);(1;3);(2;3) 这三种选择方法。根据组合数的定义,我们可以给出计算组合数 C_n^mCnm​ 的一般公式: C_n^m = \frac{n!}{m!(n-m)!}Cnm​=m!(n−m)!n!​ 其中 n! = 1 × 2 × · · · × n。(特别的,当 n = 0 时, n! = 1 ,当 m > n 时, C_n^m =0
attack
2018/04/10
7150
P3390 【模板】矩阵快速幂
题目背景 矩阵快速幂 题目描述 给定 的矩阵A,求 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k 共n行,每行n个数,第i行第j个数表示矩阵第i行第j列的元素,每个元素模10^9+7 输入输出样例 输入样例#1: 2 1 1 1 1 1 输出样例#1: 1 1 1 1 说明 , |矩阵元素|<=1000 算法:矩阵快速幂 裸题!。 注意矩阵相乘的时候tmp的值是累加的 1 #
attack
2018/04/13
5450
矩阵快速幂小结
由$n \times m$个数$a_{ij}$排成的$n$行$m$列的数表称为$n$行$m$列的矩阵,简称$n \times m$矩阵。
attack
2018/09/17
4490
矩阵快速幂小结
整数快速幂与矩阵快速幂
顾名思义,快速幂就是快速算底数的n次幂。其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高。
Cyril-KI
2022/09/19
3900
洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请你求出 f(n) mod 1000000007 的值。 输入输出格式 输入格式: ·第 1 行:一个整数 n 输出格式: 第 1 行: f(n) mod 1000000007 的值 输入输出样例 输入样例#1: 5 输出样例#1: 5 输入样例#2: 10 输出样例#2:  55 说明 对于 60% 的数据:
attack
2018/04/10
9440
xmuC语言程序实践week 1 大作业
  给定一个矩阵A,一个非负整数b和一个正整数m,求A的b次方除m的余数。   其中一个nxn的矩阵除m的余数得到的仍是一个nxn的矩阵,这个矩阵的每一个元素是原矩阵对应位置上的数除m的余数。   要计算这个问题,可以将A连乘b次,每次都对m求余,但这种方法特别慢,当b较大时无法使用。下面给出一种较快的算法(用A^b表示A的b次方):   若b=0,则A^b%m=I%m。其中I表示单位矩阵。   若b为偶数,则A^b%m=(A^(b/2)%m)^2%m,即先把A乘b/2次方对m求余,然后再平方后对m求余。   若b为奇数,则A^b%m=(A^(b-1)%m)*a%m,即先求A乘b-1次方对m求余,然后再乘A后对m求余。   这种方法速度较快,请使用这种方法计算A^b%m,其中A是一个2x2的矩阵,m不大于10000。
glm233
2020/09/28
3550
xmuC语言程序实践week 1 大作业
数论-快速幂、矩阵快速幂
文章目录 快速幂 矩阵快速幂 例题 HDU-2817 HDU-3117 快速幂 ---- image.png int fastpow(int a, int n) { int res = 1; while (n) { if (n & 1) //末位为1 res = (res * a) % mod; a = (a * a) % mod; n >>= 1; //n右移一位 } return res;
唔仄lo咚锵
2020/09/15
5420
数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂
Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only one integer T, indicates the number of tasks.
风骨散人Chiam
2020/11/06
4220
2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)
\begin{equation*} \begin{bmatrix} 1&1&1&1&1&1\\ 1 & 0&0&0&0&0\\ 0 & 0&1&3&3&1\\ 0 & 0&0&1&2&1\\ 0 & 0&0&0&1&1\\ 0 & 0&0&0&0&1\\ \end{bmatrix}^{i - 1}* \begin{bmatrix} F_{1}\\ F_0\\ 1\\ 1\\ 1\\ 1 \end{bmatrix}= \begin{bmatrix} 1&1&1&1&1&1\\ 1 & 0&0&0&0&0\\ 0 & 0&1&3&3&1\\ 0 & 0&0&1&2&1\\ 0 & 0&0&0&1&1\\ 0 & 0&0&0&0&1\\ \end{bmatrix}* \begin{bmatrix} F_{i - 1}\\ F_{i - 2}\\ i^3\\ i^2\\ i\\ 1 \end{bmatrix}= \begin{bmatrix} F_{i}\\ F_{i - 1}\\ (i + 1)^3\\ (i + 1)^2\\ i + 1\\ 1 \end{bmatrix} \end{equation*}
attack
2018/09/17
3040
2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)
HDU 2256Problem of Precision(矩阵快速幂)
求$(\sqrt{2} + \sqrt{3})^{2n} \pmod {1024}$
attack
2018/09/17
3850
矩阵快速幂
tql,ORZ,catch me off ground。 矩阵快速幂 1. 分解来看,是由矩阵乘法,和快速幂组成 矩阵乘法 for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) c[i][j]+=a[i][k]*b[k][j]; 快速幂 ll pow_ksm(ll a,ll n) { ll res = 1; while(n) { if(n&1) res = res*a%mod;
AngelNH
2020/04/16
3320
2017.11.7解题报告
预计分数:100+0+40=140 实际分数:100+0+0=100 震惊!出题人的一张机票可以用无限次 T1https://www.luogu.org/problem/show?pid=T16500
attack
2018/04/11
6180
2017.11.7解题报告
HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1534    Accepted Submission(s): 435 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给
Gxjun
2018/03/26
8400
BZOJ2476: 战场的数目(矩阵快速幂)
第一种情况:左侧或右侧有一个1,那么把这个1删去,对应的方案数为\(f[i - 1]\)
attack
2018/12/21
4350
矩阵快速幂--HDU 6030 Happy Necklace
Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads. Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads. Now Little Q wants to buy a necklace with exactly n beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 109+7. Note: The necklace is a single string, {not a circle}.
风骨散人Chiam
2020/10/28
3120
HDU 5667 Sequence(矩阵快速幂)
Problem Description Holion August will eat every thing he has found. Now there are many foods,but he does not want to eat all of them at once,so he find a sequence. fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwise He gives you 5 numbers n,a,b,c,p,and he will
ShenduCC
2018/04/26
5400
HDU 5667 Sequence(矩阵快速幂)
Fibonacci
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因[数学家]列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为兔子数列.
AngelNH
2020/04/16
4200
codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations.
风骨散人Chiam
2020/10/28
4440
相关推荐
HDU 1575 Tr A(矩阵快速幂)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档