Yet Another Number Sequence CodeForces - 392C 矩阵快速幂
2017-08-19 09:30
691 查看
题目链接:点我
Input
Output
Example
题意:
如果提上公式所说:计算 A1(k) + A2(k) + … + An(k), Ai(k) = Fi × i^k.
思路:
矩阵快速幂.
首先我们应该知道,(n+1)k =(kk)nk +(kk−1) nk−1 + (kk−2)nk−2 + ….. +(k1) n +(k0) n0
我们令u(n+1,k) = (n+1)k * F(n+1), v(n+1,k) = (n+1)k * F(n)
根据斐波那契数列递推式:
u(n+1,k) = (n+1)k * F(n+1)
= (n+1)k * F(n) + (n+1)k * F(n-1)
= ∑ki=0 (ki) * ni * F(n) + ∑ki=0 (ki) * ni * F(n-1)
= ∑ki=0 (ki) * ni * F(n) + ∑ki=0 (ki) * v(n,i)
v(n+1,k) = (n+1)k * F(n)
= ∑ki=0 (ki) * ni * F(n)
= ∑ki=0 (ki) * u(n,i)
令Sn=A1(k) + A2(k) + ... + An(k)
所以 Sn+1=Sn+u(n,k),
那么我们构造一个(2K+3) * (2k+3)的矩阵即可,这个矩阵大家根据上面的递推式在纸上写写就出来了,
代码:
Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recurrence relation: F1 = 1, F2 = 2, Fi = Fi - 1 + Fi - 2 (i > 2). We'll define a new number sequence Ai(k) by the formula: Ai(k) = Fi × i^k (i ≥ 1). In this problem, your task is to calculate the following sum: A1(k) + A2(k) + ... + An(k). The answer can be very large, so print it modulo 1000000007 (109 + 7).
Input
The first line contains two space-separated integers n, k (1 ≤ n ≤ 1e17; 1 ≤ k ≤ 40).
Output
Print a single integer — the sum of the first n elements of the sequence Ai(k) modulo 1000000007 (1e9 + 7).
Example
Input 1 1 Output 1 Input 4 1 Output 34 Input 5 2 Output 316 Input 7 4 Output 73825
题意:
如果提上公式所说:计算 A1(k) + A2(k) + … + An(k), Ai(k) = Fi × i^k.
思路:
矩阵快速幂.
首先我们应该知道,(n+1)k =(kk)nk +(kk−1) nk−1 + (kk−2)nk−2 + ….. +(k1) n +(k0) n0
我们令u(n+1,k) = (n+1)k * F(n+1), v(n+1,k) = (n+1)k * F(n)
根据斐波那契数列递推式:
u(n+1,k) = (n+1)k * F(n+1)
= (n+1)k * F(n) + (n+1)k * F(n-1)
= ∑ki=0 (ki) * ni * F(n) + ∑ki=0 (ki) * ni * F(n-1)
= ∑ki=0 (ki) * ni * F(n) + ∑ki=0 (ki) * v(n,i)
v(n+1,k) = (n+1)k * F(n)
= ∑ki=0 (ki) * ni * F(n)
= ∑ki=0 (ki) * u(n,i)
令Sn=A1(k) + A2(k) + ... + An(k)
所以 Sn+1=Sn+u(n,k),
那么我们构造一个(2K+3) * (2k+3)的矩阵即可,这个矩阵大家根据上面的递推式在纸上写写就出来了,
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long LL; const int mod = 1e9+7; int c[45][45]; LL m; struct mat{ LL a[85][85]; mat (){memset(a,0 ,sizeof(a)); } mat operator *(const mat q){ mat c; for(int i = 1; i <= m; ++i) for(int k = 1; k <= m; ++k) if(a[i][k]) for(int j = 1; j <= m; ++j){ c.a[i][j] += a[i][k] * q.a[k][j]; if (c.a[i][j] >= mod) c.a[i][j] %= mod; }return c; } }; mat qpow(mat x, LL n){ mat ans; for(int i = 1; i <= 84; ++i) ans.a[i][i] = 1; while(n){ if (n&1) ans = ans * x; x = x * x; n >>= 1; }return ans; } void init(){ for(int i = 0; i <= 41; ++i) c[i][0] = c[i][i] = 1; for(int i = 1; i <= 41; ++i) for(int j = 1; j < i; ++j) c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod; } int main(){ LL n, k; init(); scanf("%lld %lld", &n, &k); mat ans; m = 2 * k + 3; ans.a[1][1] = 1; for(int i = 2; i <= m; ++i){ ans.a[i][1] = 0; ans.a[1][i] = c[k][(i-2)%(k+1)]; }for(int i = 2; i <= k+2; ++i) for(int j = 2; j <= i;++j) ans.a[i][j] = ans.a[i][j+k+1] = ans.a[i+k+1][j] = c[i-2][j-2]; ans = qpow(ans,n-1); LL sum = 0; for(int i = 1; i <= m; ++i) sum = (sum + ans.a[1][i]) % mod; printf("%lld\n",sum); return 0; }
相关文章推荐
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水
- 7_22_G Yet another Number Sequence(矩阵快速幂)
- Codeforces 392-C Yet Another Number Sequence (矩阵快速幂)
- UVA 10689 - Yet another Number Sequence(矩阵快速幂)
- CodeForces 392 C.Yet Another Number Sequence(矩阵快速幂)
- Yet another Number Sequence UVA - 10689 (矩阵快速幂)
- 【矩阵快速幂】UVA 10698 G - Yet another Number Sequence
- 矩阵的运算 --- 矩阵快速幂(UVA10689 - Yet another Number Sequence)
- uva 10689 Yet another Number Sequence【矩阵快速幂】
- UVA 10698 Yet another Number Sequence 矩阵快速幂
- 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)
- uva 10689 - Yet another Number Sequence(矩阵快速幂)
- Yet Another Number Sequence——[矩阵快速幂]
- UVA-10689 Yet another Number Sequence (矩阵二分幂模板)
- Educational Codeforces Round 40 (Rated for Div. 2) F. Runner's Problem 前缀和求区间覆盖 矩阵快速幂优化DP 离散化
- Yet Another Number Sequence
- Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence