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. [TensorFlow笔记乱锅炖] tf.multinomial(logits, num_samples)使用方法

    tf.multinomial(logits, num_samples) 第一个参数logits可以是一个数组,每个元素的值可以简单地理解为对应index的选择概率,注意这里的概率没有规定加起来的和为1 ...

  2. 【转】Python基础-封装与扩展、静态方法和类方法

    [转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...

  3. vim7.4+python3配置

    基本参考:https://blog.csdn.net/qq_26877377/article/details/80717755 注意几点: (1)关于python的自动补全,不要使用pydiction ...

  4. 算法:60.第k个排列

    解答参考:https://blog.csdn.net/lqcsp/article/details/23322951 题目链接:https://leetcode-cn.com/problems/perm ...

  5. V4L2 API详解 <二> Camera详细设置【转】

    转自:http://blog.sina.com.cn/s/blog_602f87700101bf36.html 作者: Sam (甄峰)  sam_code@hotmail.com   Camera的 ...

  6. bootstrap登录界面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  7. boost.lexical_cast 学习

    1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typen ...

  8. Django-jinjia2的赋值

    一 变量 1. 变量的形式是:{{ variable }},当模板引擎碰到变量时,引擎使用变量的值替代变量: 2. 使用“.”能够访问变量的属性: 3. 当模板引擎碰到“.”的时候,查找顺序是: a) ...

  9. 缓存系列之一:buffer、cache与浏览器缓存

    缓存系列之一:buffer.cache与浏览器缓存 一:缓存是为了调节速度不一致的两个或多个不同的物质的速度,在中间对速度较快的一方起到一个加速访问速度较慢的一方的作用,比如CPU的一级.二级缓存是保 ...

  10. php 汉字首字母和全拼

    <?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...