题解 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 ...
随机推荐
- 最新 .NET Core 中 WebSocket的使用 在Asp.Net MVC 中 WebSocket的使用 .NET Core 中 SignalR的使用
ASP.NET MVC 中使用WebSocket 笔记 1.采用控制器的方法 这个只写建立链接的方法的核心方法 1.1 踩坑 网上都是直接 传个异步方法 直接接受链接 自己尝试了好多次链接是打开的,到 ...
- MVC模式职责分工及学习路上的一些感想
在正文之前想先说说自己coding道路上的一点感想,不得不感慨一下时间过得很快,之前写过一篇关于JavaWeb_MVC模式的一篇博客,转眼之间时间已经过去了两个月,那时候还是一个刚刚接触JavaWeb ...
- client-go实战之三:Clientset
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 20210712 noip12
考场 第一次和 hzoi 联考,成功给 sdfz 丢人 尝试戴耳罩,发现太紧了... 决定改变策略,先用1h看题,想完3题再写. T1 一下想到枚举最大值,单调栈求出每个点能作为最大值的区间,然后以这 ...
- Spring Dependency Injection浅析
Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件. 1.在UserService中提供一个get/set的name方法, ...
- Selenium系列4-元素定位
前言 说起元素定位,一定是学习自动化测试绕不开的第一道关,无论是web端的UI自动化还是移动端的自动化,在需要首先对元素进行定位才可以完成对元素的操作已达成测试目的,在Selenium中,可以使用fi ...
- 羽夏看Win系统内核——环境搭建
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
- php各个版本curl上传文件的兼容实现
// 以POST方式提交数据 public function post_data($url, $param, $is_file = false, $return_array = true) { ... ...
- java web 项目中web.xml 详解
web.xml详述: web.xml 是每个Java web 项目的必备文件,又叫做部署描述符,Servlet规范中定义的,是web应用的配置文件. 概念: .部署描述符文件就像所有XML文件一样,必 ...
- Percolator模型及其在TiKV中的实现
一.背景 Percolator是Google在2010年发表的论文<Large-scale Incremental Processing Using Distributed Transactio ...