题解 Yet Another Number Sequence
Description
给出 \(n,k\) ,求出:
\]
其中 \(f_i\) 表示斐波拉契第 \(i\) 项。\(n\le 10^{18},k\le 40\) 。
Solution
哎,被这个 \(k\le 40\) 误导了,以为是斯特林数。/kk
发现假如我们用矩阵表示 \(f_i\),那么我们就只需要求出:
\]
其中 \(G=\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}\) 。
考虑倍增,设 \(f(n,m)=\sum_{i=1}^{n} G^ii^m\),则有:
\]
然后我们就可以做到 \(\Theta(k^2\log n)\) 了,不过似乎分治的时候用 \(\text{fft}\) 的话可以做到 \(\Theta(k\log k\log n)\),不过这个题中 \(k\) 不是很大,所以可以接受。
(矩阵的 \(\text{fwt}\) 可以对于每一项搞)
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define mod 1000000007
#define int long long
#define MAXN 45
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int k,C[MAXN][MAXN];
int mul (int a,int b){return 1ll * a * b % mod;}
int dec (int a,int b){return a >= b ? a - b : a + mod - b;}
int add (int a,int b){return a + b >= mod ? a + b - mod : a + b;}
void Add (int &a,int b){a = add (a,b);}
void Sub (int &a,int b){a = dec (a,b);}
struct matrix{
int mat[2][2];
matrix(){memset (mat,0,sizeof (mat));}
int * operator [] (const int key){return mat[key];}
matrix operator * (const matrix &p)const{
matrix now;
for (Int i = 0;i < 2;++ i)
for (Int j = 0;j < 2;++ j)
for (Int k = 0;k < 2;++ k)
Add (now.mat[i][k],mul (mat[i][j],p.mat[j][k]));
return now;
}
matrix operator * (const int &p)const{
matrix now;
for (Int i = 0;i < 2;++ i)
for (Int j = 0;j < 2;++ j)
now.mat[i][j] = mul (mat[i][j],p);
return now;
}
matrix operator + (const matrix &p)const{
matrix now;
for (Int i = 0;i < 2;++ i)
for (Int j = 0;j < 2;++ j)
now.mat[i][j] = add (mat[i][j],p.mat[i][j]);
return now;
}
}one,ret;
struct node{
matrix gn,f[MAXN];
};
node getit (int n){
node now;
if (n == 1){
now.gn = ret;
for (Int i = 0;i <= k;++ i) now.f[i] = ret;
return now;
}
if (n & 1){
node lst = getit (n - 1);
now.gn = lst.gn * ret;
for (Int i = 0,res = 1;i <= k;++ i,res = mul (res,n % mod)) now.f[i] = lst.f[i] + now.gn * res;
}
else{
int mid = n >> 1;
node lst = getit (mid);now.gn = lst.gn * lst.gn;
for (Int m = 0;m <= k;++ m){
now.f[m] = lst.f[m];
for (Int h = 0,res = 1;h <= m;++ h,res = mul (res,mid % mod)) now.f[m] = now.f[m] + (lst.gn * lst.f[m - h]) * mul (res,C[m][h]);
}
}
return now;
}
int n;
signed main(){
read (n,k),one[0][0] = one[1][1] = 1,ret[0][0] = ret[0][1] = ret[1][0] = C[0][0] = 1;
for (Int i = 1;i <= k;++ i){
C[i][0] = 1;
for (Int j = 1;j <= i;++ j) C[i][j] = add (C[i - 1][j - 1],C[i - 1][j]);
}
node now = getit (n);
write (now.f[k][0][0]),putchar ('\n');
return 0;
}
题解 Yet Another Number Sequence的更多相关文章
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- Number Sequence (HDoj1005)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- Number Sequence(kmp)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Number Sequence(周期是336!!不是48!!)
1005 Number Sequence 时间限制: 1 Sec 内存限制: 60 MB 题目描述 A number sequence is defined as follows: f(1) = 1 ...
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
Number Sequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Pro ...
- Winter-1-F Number Sequence 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description A number sequence is defined as follows:f(1) ...
- Poj 1019 Number Sequence( 数据分析和操作)
一.题目大意 有这样一个序列包含S1,S2,S3...SK,每一个Si包括整数1到 i.求在这个序列中给定的整数n为下标的数. 例如,前80位为1121231234123451234561234567 ...
- CodeForces - 393E Yet Another Number Sequence
Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
随机推荐
- docker实现mysql主从复制
目录 一.概述 二.创建master主库 三.创建Slave实例 四.主从配置 五.参考 一.概述 1.原理 master服务器将数据的改变记录二进制binlog日志,当master上的数据发生改变时 ...
- jquery mobile cdn
<head> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jque ...
- C#多线程开发-线程同步 02
上一篇文章主要带领大家认识了线程,也了解到了线程的基本用法和状态,接下来就让我们一起学习下什么是线程同步. 线程中异常的处理 在线程中始终使用try/catch代码块是非常重要的,因为不可能在线程代码 ...
- vue 引用省市区三级联动(插件)
vue 用省市区三级联动之傻瓜式教程(复制粘贴即用) npm 下载 npm install v-distpicker --save main.js //引入 省市区三级联动 import Distpi ...
- 【Python机器学习实战】决策树与集成学习(五)——集成学习(3)GBDT应用实例
前面对GBDT的算法原理进行了描述,通过前文了解到GBDT是以回归树为基分类器的集成学习模型,既可以做分类,也可以做回归,由于GBDT设计很多CART决策树相关内容,就暂不对其算法流程进行实现,本节就 ...
- python库--pandas--Series.str--字符串处理
原数据 import pandas as pd a = pd.Series(['aSd', 'asd', 'dfd fsAsf sfs']) b = pd.Series([None, 'asd', ' ...
- CentOS获取公网IP
Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo. ...
- 集合Collection ----Set集合
set系列集合:添加的元素是 无序,不重复,无索引的 ----HashSet: 无序,不重复,无索引 ------LinkHashSet<>:有序不重复无索引(添加顺序) ----Tree ...
- BF算法(串模式匹配算法)
主串和子串 主串与子串:如果串 A(如 "shujujiegou")中包含有串 B(如 "ju"),则称串 A 为主串,串 B 为子串.主串与子串之间的关系可简 ...
- iNeuLink硬件网关与iNeuOS工业互联网操作系统互联互通应用案例
目 录 1. 应用概述... 2 2. 模拟硬件设备配置... 2 3. iNeuLink硬件网关配置... 4 3.1 硬件介绍... ...