【洛谷】P1962
题目链接: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的更多相关文章
- 洛谷P1962 斐波那契数列【矩阵运算】
洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...
- 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]
P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...
- 洛谷 P1962 斐波那契数列
题目链接:https://www.luogu.org/problemnew/show/P1962 题目大意: 略 分析: 由于数据规模很大,需要用矩阵快速幂来解. 代码如下: #pragma GCC ...
- 【洛谷P1962】斐波那契数列
斐波那契数列 题目链接:https://www.luogu.org/problemnew/show/P1962 矩阵A 1,1 1,0 用A^k即可求出feb(k). 矩阵快速幂 #include&l ...
- 洛谷——P1962 斐波那契数列
P1962 斐波那契数列 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 ...
- 题解——洛谷P1962 斐波那契数列(矩阵乘法)
矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...
- AC日记——斐波那契数列 洛谷 P1962
斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...
- 洛谷P1962 斐波那契数列
传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...
- 洛谷—— P1962 斐波那契数列
https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
随机推荐
- Java-Class-@I:org.springframework.web.bind.annotation.PostMapping
ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.PostMapping 1.返回顶部 2.返回顶部 1. package ...
- PHP面试 PHP基础知识 九(面向对象)
面向对象 PHP的类权限控制修饰符 public(公共的) . protected(受保护的).private(私有的) public :最高权限 可以在类的内部使用 可以在类的外部使用 可以 ...
- docker常用
1. docker 查看正在运行 2. docker 查看所有 3. docker 查看镜像 4. docker 运行使用 docker run --name 镜像名(自己起的镜像名字)-d(后台) ...
- 前端(七)—— 盒模型之display、overflow、隐藏、border、margin、样式支持,层级结构
display.overflow.隐藏.border.margin.样式支持,层级结构 一.盒模型之display 1.三种样式 block 块 inline 内联/行内 inline-block 内 ...
- 【二】Jmeter接口自动化测试系列之函数使用及扩展
上一篇文章我们了解了Jmeter的参数化的集中方法,虽然方法不是很多,但已经足够使用! 本篇文章,介绍一下Jmeter自带函数的使用和 函数扩展,来满足测试工作中的各种需求! Jmeter自带函数 点 ...
- zabbix--监控的组件和进程介绍
上图是zabbix的架构,zabbix proxy(代理),可以减小IO并发. zabbix web GUI是用php写的画图工具,从数据库抓取数据. zabbix database zabbix获取 ...
- minicom 的使用
1.切换到 root 用户. [root@localhost btools]#su - 2. 查找有效的串设备. [root@localhost ~]#cat /proc/devices ... 4 ...
- windows 开启管理员权限
在使用cmd为windows系统的电脑添加一条路由的时候,发现提示我权限不足,经过我的查找,需要在 我的电脑 右键 管理 本地用户管理 打开用户一栏 找到管理员账户 右键打开属 ...
- wireshark抓包总结
过滤规则: ip.src == 源地址 and http(协议)
- vue v-for 组件传值 enter传值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...