Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 3832    Accepted Submission(s): 1662

Problem Description

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 b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. 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. t test cases follow.

Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.

Output

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

Sample Input

2

3 1 2

4 1 10

Sample Output

85

369

HintIn the first case, the third number is 85 = 2*1十2十3^4.

 In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

Source

2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

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

f(n) 1 2 1 0 0 0 0 f(n-1)
f(n-1) 1 0 0 0 0 0 0 f(n-2)
(n+1)^4 0 0 1 4 6 4 1 n^4
(n+1)^3 0 0 0 1 3 3 1 n^3
(n+1)^2 0 0 0 0 1 2 1 n^2
(n+1) 0 0 0 0 0 1 1 n
1 0 0 0 0 0 0 1 1
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 2147493647
#define ll long long
using namespace std;
struct mat{
ll t[7][7];
mat(){
memset(t,0,sizeof(t));
}
mat operator*(mat b){
mat c;
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
for(int k=0;k<7;k++)
c.t[i][j]=(c.t[i][j]%inf+t[i][k]*b.t[k][j])%inf;
return c;
}
};
mat pow(int nn,mat B,mat A)
{
while(nn){
if(nn%2==1)
B=A*B;
A=A*A;
nn/=2;
}
return B;
}
int main()
{
int T,n;
ll a[7][7]=
{1,2,1,0,0,0,0,
1,0,0,0,0,0,0,
0,0,1,4,6,4,1,
0,0,0,1,3,3,1,
0,0,0,0,1,2,1,
0,0,0,0,0,1,1,
0,0,0,0,0,0,1};
mat A;
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
A.t[i][j]=a[i][j];
mat B;
B.t[2][0]=81;
B.t[3][0]=27;
B.t[4][0]=9;
B.t[5][0]=3;
B.t[6][0]=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%lld%lld",&n,&B.t[1][0],&B.t[0][0]);
if(n==1)
printf("%lld\n",B.t[1][0]);
else if(n==2)
printf("%lld\n",B.t[0][0]);
else{
mat C=pow(n-2,B,A);
printf("%lld\n",C.t[0][0]%inf);
}
}
return 0;
}

HDU5950 Recursive sequence (矩阵快速幂)的更多相关文章

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

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

  2. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  3. 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 ...

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

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  5. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  6. hdu 5950 Recursive sequence 矩阵快速幂

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

  7. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  8. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  9. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

随机推荐

  1. gitlab的配置

    一. 管理员配置 gitlab 1. 登录 gitlab 等待 docker 容器启动完成后, 登陆 http://localhost:8080 第一次访问是让我们修改管理员密码.如下所示 初始化 g ...

  2. Python备份MySQL数据库【转】

    #!/usr/bin/env python # coding: utf- import os import time ''' defined variable ''' databases=['hch' ...

  3. use snippet save dom to excel

    1.打开贴吧 http://tieba.baidu.com/f?ie=utf-8&kw=python&fr=search 2.打开console执行下面命令 Array.prototy ...

  4. 007_Chrome的Waterfall详解

    一. 浏览器根据html中外连资源出现的顺序,依次放入队列(Queue),然后根据优先级确定向服务器获取资源的顺序.同优先级的资源根据html中出现的先后顺序来向服务器获取资源 Queueing. 出 ...

  5. appium+java报错之nodejs报错

    $ gulp(node:784) fs: re-evaluating native module sources is not supported. If you areusing the grace ...

  6. 腾讯云CVM之间配置免密钥登录

    背景: 1客户A和B俩台主机之间需要实现免密钥登录,已绑定腾讯云申请的密钥对 系统:centos7.3 A:192.168.0.100 B:192.168.0.84 A主机的私钥文件:aaa B主机的 ...

  7. JS调用摄像头并上传图片到服务器

    本功能只能把图片转成base64码上传,如何上传图片还没有修改出来,有兴趣的朋友弄出来了,请给我留下言,谢谢了! 直接上代码,需要的朋友直接复制就可以使用了. <!DOCTYPE html> ...

  8. Go语言环境安装&搭建(Win)

    Go语言简介 什么是Go语言 Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持 ...

  9. Codeforces 487E Tourists [广义圆方树,树链剖分,线段树]

    洛谷 Codeforces 思路 首先要莫名其妙地想到圆方树. 建起圆方树后,令方点的权值是双联通分量中的最小值,那么\((u,v)\)的答案就是路径\((u,v)\)上的最小值. 然而这题还有修改, ...

  10. mysql运维

    反反复复装了好多次的mysql,上学的时候从来没有考虑过稳定性,装起来,能跑通,增删改查没有问题万事大吉.参与工作后参与平台搭建和维护,平台的稳定性是首先必须要考虑的问题,之前装mysql使用经历了密 ...