51nod 浮波那契(构造矩阵+矩阵快速幂)
来源:互联网 发布:无限极网络电视盒 编辑:程序博客网 时间:2024/05/22 14:17
TengBieBie已经学习了很多关于斐波那切数列的性质,所以他感到一些些厌烦。现在他遇到了一个新的数列,这个数列叫做Float-Bonacci。这里有一个关于Float-Bonacci的定义。对于一个具体的n,TengBieBie想要快速计算FB(n).但是TengBieBie对FB的了解非常少,所以他向你求助。你的任务是计算FB(n).FB(n)可能非常大,请输出FB(n)%1,000,000,007 (1e9+7)即可。Input输入共一行,在一行中给出一个整数n (1<=n<=1,000,000,000)。Output对于每一个n,在一行中输出FB(n)%1,000,000,007 (1e9+7)。Input示例5Output示例2
额,傻逼题,怪自己智障。fb(n)=fb(n-1)+fb(n-3.4)怎么化呢,我们构造矩阵的时候都是n-c(c为整数),那我们就要把n-3.4化成整数形式我们推出来f(x*n)=f(x*(n-1))+f(x*(n-3.4)),这里x是5的倍数就可以了。直观点我们就乘以10,化成f(n)=f(n-10)+f(34),那么,我们就有fb(n)=f(10*n).推出这个我们就可以来推出矩阵:| |*|f(n-1) | = | f(n) ||1 | |f(n-2) | |f(n-1) || 1 | |f(n-3) | |f(n-2) || 1 | |.......| |.......|| ....... | |f(n-33)| |f(n-32)|| 1| |f(n-34)| |f(n-33)|
#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cctype>#include<cmath>#include<ctime>#include<string>#include<stack>#include<deque>#include<queue>#include<list>#include<set>#include<map>#include<cstdio>#include<limits.h>#define MOD 1000000007#define fir first#define sec second#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)#define mes(x, m) memset(x, m, sizeof(x))#define Pii pair<int, int>#define Pll pair<ll, ll>#define INF 1e9+7#define Pi 4.0*atan(1.0)#define lowbit(x) (x&(-x))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long ll;typedef unsigned long long ull;const double eps = 1e-12;const int maxn = 35;using namespace std;inline int read(){ int x(0),f(1); char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}struct matrix{ ll mat[maxn][maxn]; void init(){ mes(mat, 0); for(int i = 0; i < 35; ++i){ mat[i][i] = 1; } } void clear(){ mes(mat, 0); } void output(){ for(int i = 0; i < 35; ++i){ for(int j = 0; j < 35; ++j){ // printf("%lld ", mat[i][j]); cout << mat[i][j] << " "; } printf("\n"); } } matrix operator*(const matrix &base){ matrix tmp; tmp.clear(); for(int i = 0; i < 35; ++i){ for(int j = 0; j < 35; ++j){ for(int k = 0; k < 35; ++k){ tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*base.mat[k][j]); tmp.mat[i][j] %= MOD; } } } return tmp; }};matrix matrix_fast_mod(ll m, matrix base){ matrix res; // res.output(); res.init(); while(m){ if(m&1){ res = res*base; } base = base*base; m >>= 1; } return res;}int main(){ //int N; // N = read(); ll N; scanf("%lld", &N); matrix base; matrix p; p.clear(); for(int i = 1; i < 35; ++i){ p.mat[i][1] = 1; } base.clear(); base.mat[1][10] = base.mat[1][34] = 1; for(int i = 2; i < 35; ++i){ base.mat[i][i-1] = 1; } if(N < 5){ printf("1\n"); } else{ base = matrix_fast_mod((N-4)*10, base); p = base*p; printf("%lld\n", p.mat[1][1]); } return 0;}
0 0
- 51nod 1358 浮波那契 (类斐波那契数列+矩阵快速幂+构造矩阵)
- 51nod 浮波那契(构造矩阵+矩阵快速幂)
- 51Nod 1358 浮波那契 (矩阵快速幂 构造矩阵)
- 51Nod 1358 浮波那契 (矩阵快速幂)
- 51nod 1358:浮波那契 构造矩阵
- 51nod-1358-浮波那契(构造矩阵)
- 51nod 1358 浮波那契【思维+矩阵快速幂】好题!
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项【矩阵快速幂】
- 51nod 1242 斐波那契数列的第N项 (矩阵快速幂)
- 【51Nod】1242 - 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51Nod-斐波那契数列的第N项(矩阵快速幂)
- [51NOD]-1242 斐波那契数列的第N项 [矩阵快速幂]
- 51 nod 1242 斐波那契数列的第N项 矩阵快速幂
- 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 矩阵快速幂 51nod
- 51nod 巨大的斐波那契数列(矩阵快速幂),递推式优化的好模板!!!!!!!
- 设计模式之单例模式:singleton
- java File类
- nginx-http模块的数据结构
- 动态代理两种实现方式
- 判断一棵树
- 51nod 浮波那契(构造矩阵+矩阵快速幂)
- 树:二叉树的前序/中序/后序/层次递归
- 树:二叉树的公共祖父节点
- 数据结构:图的代码
- 布隆过滤器:实现代码
- JavaScript中‘this’关键词的优雅解释
- 位图:源代码
- 大/小堆:源代码
- 智能指针