题目

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number bb. After that, the ii-th cow says the sum of twice the (i−2)-th number, the (i−1)-th number, and i^4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Input

The first line of input contains an integer t, the number of test cases. tt test cases follow.

Each case contains only one line with three numbers N, aa and bb where N,a,b<2^31 as described above.

Output

For each test case, output the number of the NN-th cow. This number might be very large, so you need to output it modulo 2147493647.

In the first case, the third number is $85 = 21+2+3^4.Inthesecondcase,thethirdnumberis.Inthesecondcase,thethirdnumberis93 = 21+1*10+3^4andthefourthnumberisandthefourthnumberis369 = 2 * 10 + 93 + 4^4$.

题意

题目给出这样一个递推公式:

f(n)=f(n-2)*2+f(n-1)+n^ 4

f(1)=a,

f(2)=b;

给出n,a,b求f(n)%2147493647;

根据递推公式容易得出一个O(N)的暴力算法。但这题的数据范围为N,a,b<2^31 ,直接暴力肯定超时,要用O(1),O(lngn)或者是O(n^(1/2))的算法才有可能ac

观察这种递推公式,发现我们可以使用矩阵快速幂来计算,矩阵快速幂的复杂度是O(m^3 *lngn)m是矩阵的大小,复杂度很小可以一试。

矩阵快速幂是定义一个状态矩阵和一个转移矩阵,前一个状态矩阵乘转移矩阵可得到后一个状态矩阵。

这题和普通的矩阵快速幂有点区别,这题的递推函数在混合了一个变量n^4。

所以我们这题首先要考虑的就是,如何从n^4推出(n+1)^4,我们发现通过二项式定理可以得出,(n+1)^4=n^4+4n^3+6n^2+4n+1

这样就有f(n+1)=2*f(n-1)+f(n)+n^4+4n^3+6n^2+4n+1

不妨设状态矩阵F(n-1)=[f(n-1),f(n),n^4,4n^3,6n^2,4n,1]

若F(n)=F(N-1)*A 即[f(n),f(n+1),(n+1)^4,4(n+1)^3,6(n+1)^2,4(n+1),1]=[f(n-1),f(n),n^4,4n^3,6n^2,4n,1]*A

计算得A=

{0,2,0,0,0,0,0}
{1,1,0,0,0,0,0}
{0,1,1,0,0,0,0}
{0,1,1,1,0,0,0}
{0,1,1,2,1,0,0}
{0,1,1,3,3,1,0}
{0,1,1,4,6,4,1}

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const ll mod=2147493647;
ll n; void mul(ll f[7],ll a[7][7]){
ll c[7];
memset(c,0,sizeof(c));
for(ll j=0;j<7;j++)
for(ll k=0;k<7;k++)
c[j]=(c[j]+(ll)f[k]*a[k][j])%mod;
memcpy(f,c,sizeof(c));
} void mulself(ll a[7][7]){
ll c[7][7];
memset(c,0,sizeof(c));
for(ll i=0;i<7;i++)
for(ll j=0;j<7;j++)
for(ll k=0;k<7;k++)
c[i][j]=(c[i][j]+(ll)a[i][k]*a[k][j])%mod;
memcpy(a,c,sizeof(c)); } int main(){
ll t,a,b;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld%lld",&n,&a,&b);
ll f[]={a,b,16,32,24,8,1};
ll a[][7]={
{0,2,0,0,0,0,0},
{1,1,0,0,0,0,0},
{0,1,1,0,0,0,0},
{0,1,1,1,0,0,0},
{0,1,1,2,1,0,0},
{0,1,1,3,3,1,0},
{0,1,1,4,6,4,1},
};
n--;
for(;n;n>>=1){
if(n&1)mul(f,a);
mulself(a);
}
printf("%lld\n",f[0]); } }

Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HDU5950 Recursive sequence (矩阵快速幂)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. 2016ACM/ICPC亚洲区沈阳站 - A/B/C/E/G/H/I - (Undone)

    链接:传送门 A - Thickest Burger - [签到水题] ACM ICPC is launching a thick burger. The thickness (or the heig ...

  6. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Thickest Burger Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

随机推荐

  1. label 的autosize属性

    label 的autosize属性,显示不全的问题.

  2. Node中间层浅认知

    Node中间层允许前端来做网站路由.页面渲染.SEO优化,对以往从来不接触这些内容的前端选手来说,正是锻炼我们网站架构的好机会.另外,这也是一次深入了解Node的好机会,准备好迎接即将到来的前端工程化 ...

  3. 初学WebGL引擎-BabylonJS:第4篇-灯光动画与丛林场景

    前几章接触的案例都是接近静态的,由这张开始开始接触大量动态的内容,包括 球体灯光,变动的形体,以及一个虚拟的丛林场景 下章我会试着结合1-9案例的内容做出一个demo出来 [playground]-l ...

  4. HashMap位运算你可知一二

    前置位运算知识 我们平时在写代码过程中用的位运算操作比较少,因为我们更关注于可读性而不是性能,如果为了性能而使用较多的位运算,我想我们的同事会疯掉.但在框架里位运算却非常常见,因为框架的性能是我们关注 ...

  5. 状压dp:luogu P2704 [NOI2001]炮兵阵地

    https://www.luogu.org/problemnew/show/P2704 知识点:1.滚动数组:取模实现 2.位运算优先级最低 顾是if(!(a&b))而不是if(!a& ...

  6. DSRC和USRP的购买调研

    (转移自旧博客) 11.29 2019 实验室采购,所以进行了一定程度的调研. 主要包括两个Part,分别是DSRC和USRP的简单总结,购买建议和详细资料. Part.1 DSRC调研总结 1.1 ...

  7. 使用Java7提供的WatchService给目录添加新建文件监控

    程序: import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import ...

  8. MyBatis实现与插件开发

    分析源码之前也需要源码下载并安装到本地仓库和开发工具中,方便给代码添加注释:安装过程和mybatis源码的安装过程是一样的,这里就不再重复描述了:下载地址:https://github.com/myb ...

  9. whlie do-whlie

    switch语句  用于根据多个不同条件执行不同动作.   while 循环         while循环基本语法:    条件初始化;   while(条件表达式){     //条件表达式就是判 ...

  10. 一文带你了解Sql优化

    我们后台开发人员每天都难免与数据库打交道,那么你在写sql语句的时候有注重到自己sql的效率吗?当你sql查询速度很慢的时候你有想过是你的sql语句造成的吗?看完这篇文章,我相信你会对sql优化有了一 ...