题目链接:https://www.luogu.org/problemnew/show/P1962

题意:求fib数列的第n项,很大。mod 1e9+7.

题解:BM直接推。

代码:

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = ;
ll powmod(ll a, ll b)
{
ll res = ; a %= mod;
assert(b >= );
for (; b; b >>= )
{
if (b & )
res = res * a%mod;
a = a * a%mod;
}
return res;
}
// head int _, n;
namespace linear_seq
{
const int N = ;
ll res[N], base[N], _c[N], _md[N];
vector<int> Md;
void mul(ll *a, ll *b, int k)
{
rep(i, , k + k) _c[i] = ;
rep(i, , k)
if (a[i])
rep(j, , k)
_c[i + j] = (_c[i + j] + a[i] * b[j]) % mod;
for (int i = k + k - ; i >= k; i--)
if (_c[i])
rep(j, , SZ(Md))
_c[i - k + Md[j]] = (_c[i - k + Md[j]] - _c[i] * _md[Md[j]]) % mod;
rep(i, , k) a[i] = _c[i];
}
int solve(ll n, VI a, VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
// printf("%d\n",SZ(b));
ll ans = , pnt = ;
int k = SZ(a);
assert(SZ(a) == SZ(b));
rep(i, , k)
_md[k - - i] = -a[i]; _md[k] = ;
Md.clear();
rep(i, , k)
if (_md[i] != ) Md.push_back(i);
rep(i, , k)
res[i] = base[i] = ;
res[] = ;
while ((1ll << pnt) <= n) pnt++;
for (int p = pnt; p >= ; p--)
{
mul(res, res, k);
if ((n >> p) & )
{
for (int i = k - ; i >= ; i--) res[i + ] = res[i]; res[] = ;
rep(j, , SZ(Md)) res[Md[j]] = (res[Md[j]] - res[k] * _md[Md[j]]) % mod;
}
}
rep(i, , k) ans = (ans + res[i] * b[i]) % mod;
if (ans < ) ans += mod;
return ans;
}
VI BM(VI s)
{
VI C(, ), B(, );
int L = , m = , b = ;
rep(n, , SZ(s))
{
ll d = ;
rep(i, , L + ) d = (d + (ll)C[i] * s[n - i]) % mod;
if (d == ) ++m;
else if ( * L <= n)
{
VI T = C;
ll c = mod - d * powmod(b, mod - ) % mod;
while (SZ(C) < SZ(B) + m) C.pb();
rep(i, , SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod;
L = n + - L; B = T; b = d; m = ;
}
else
{
ll c = mod - d * powmod(b, mod - ) % mod;
while (SZ(C) < SZ(B) + m) C.pb();
rep(i, , SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod;
++m;
}
}
return C;
}
int gao(VI a, ll n)
{
VI c = BM(a);
c.erase(c.begin());
rep(i, , SZ(c)) c[i] = (mod - c[i]) % mod;
return solve(n, c, VI(a.begin(), a.begin() + SZ(c)));
}
}; int main() {
long long n;
vector<int>v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
while(~scanf("%lld",&n)){
printf("%lld\n", linear_seq::gao(v, n - ));
//VI{1,2,4,7,13,24}
//printf("%lld\n", linear_seq::gao(v, n - 1));
//printf("%d\n",linear_seq::gao(VI{x1,x2,x3,x4},n-1));
}
}

update:

矩阵快速幂

\begin{pmatrix} 1 & 1\\ 1 & 0 \end{pmatrix}  *

\begin{pmatrix} f(n-1)\\ f(n-2) \end{pmatrix} =

\begin{pmatrix} f(n)\\ f(n-1) \end{pmatrix}

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = ;
const ll mod = 1e9+; ll n,p; //矩阵结构体
struct Matrix{
ll a[maxn][maxn];
void init(){ //初始化为单位矩阵
memset(a, , sizeof(a));
for(int i = ; i <= maxn;i++){
a[i][i] = ;
}
}
}; //矩阵乘法
Matrix mul(Matrix a, Matrix b){
Matrix ans;
for(int i = ;i <= ;++i){
for(int j = ;j <= ;++j){
ans.a[i][j] = ;
for(int k = ;k <= ;++k){
ans.a[i][j] = ans.a[i][j] % mod + a.a[i][k] * b.a[k][j] % mod;
}
}
}
return ans;
} //矩阵快速幂
Matrix qpow(Matrix a,ll b){
Matrix ans;
ans.init();
while(b){
if(b & )
ans = mul(ans,a);
a = mul(a,a);
b >>= ;
}
return ans;
} void print(Matrix a){
for(int i = ; i <= n;++i){
for(int j = ;j <= n;++j){
cout << a.a[i][j]%mod<< " ";
}
cout << endl;
}
} int main(){
Matrix base;
Matrix ans;
ans.a[][] = ;
ans.a[][] = ;
base.a[][] = ;
base.a[][] = ;
base.a[][] = ;
base.a[][] = ;
cin>>n;
ans = mul(ans,qpow(base,n-));
cout<<ans.a[][]<<endl;
return ;
}

【洛谷】P1962的更多相关文章

  1. 洛谷P1962 斐波那契数列【矩阵运算】

    洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...

  2. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

  3. 洛谷 P1962 斐波那契数列

    题目链接:https://www.luogu.org/problemnew/show/P1962 题目大意: 略 分析: 由于数据规模很大,需要用矩阵快速幂来解. 代码如下: #pragma GCC ...

  4. 【洛谷P1962】斐波那契数列

    斐波那契数列 题目链接:https://www.luogu.org/problemnew/show/P1962 矩阵A 1,1 1,0 用A^k即可求出feb(k). 矩阵快速幂 #include&l ...

  5. 洛谷——P1962 斐波那契数列

    P1962 斐波那契数列 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 ...

  6. 题解——洛谷P1962 斐波那契数列(矩阵乘法)

    矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...

  7. AC日记——斐波那契数列 洛谷 P1962

    斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...

  8. 洛谷P1962 斐波那契数列

    传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...

  9. 洛谷—— P1962 斐波那契数列

    https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...

  10. 洛谷P1962 斐波那契数列(矩阵快速幂)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...

随机推荐

  1. 入门级_Tensorflow网络搭建

    Tensorflow如何搭建神经网络 1.基本概念 基于Tensorflow的神经网络:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型 张量:张量就是多维数据 ...

  2. HDU 6693 Valentine's Day (概率)

    2019 杭电多校 10 1003 题目链接:HDU 6693 比赛链接:2019 Multi-University Training Contest 10 Problem Description O ...

  3. Servilet初步

    以http://locahost:8080/......开头,或者以/开头,都是绝对路径以路径开头:相对路径 路径/路径 Servlet执行流程:(只用自己编写执行的代码,执行的细节全是tomcat封 ...

  4. js小项目:显示与输入的内容相关的

    1,添加键盘抬起事件 2,获取文本框的内容,是否与数组中的内容匹配 3,创建元素 <!DOCTYPE html> <html lang="en"> < ...

  5. keepAlived主备及双主

    nginx用默认配置即可 1.主备配置 1.主keepAlived配置 vrrp_instance VI_1 { state MASTER #主备区分 interface eth0 virtual_r ...

  6. springcloud -zuul(2-执行流程及源码)

    官方图 1.Servlet zuul.servletPath默认配置为/zuul,故请求为/zuul开头的会跳过dispatcherServlet直接进入ZuulServlet,该配置可以自定义配置, ...

  7. thinkphp 多语言支持

    ThinkPHP内置多语言支持,如果你的应用涉及到国际化的支持,那么可以定义相关的语言包文件.任何字符串形式的输出,都可以定义语言常量. 要启用多语言功能,需要配置开启多语言行为,在应用的配置目录下面 ...

  8. luoguP1134 阶乘问题 [数论]

    题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...

  9. 累乘函数线性逆元打表,阶乘反演——bzoj4816

    学了一种新套路,倒序打表函数的逆元可以直接线性完成 #include<bits/stdc++.h> using namespace std; #define ll long long #d ...

  10. fastJson中常用方法以及遇到的“坑”

    1.使用fastJson,首先引入fastJson依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> & ...