Recursive sequence

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

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

Hint

In 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
  套板子,取模开ll就好了;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,MOD=;
struct Matrix
{
ll a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+(a[i][k]*B.a[k][j])%MOD)%MOD;
return C;
}
Matrix operator ^ (const ll &t)const
{
Matrix A=(*this),res;
res.init();
int p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
Matrix base,hh;
void init()
{
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
}
void init1(ll a,ll b)
{
memset(hh.a,,sizeof(hh.a));
hh.a[][]=b%MOD;
hh.a[][]=a%MOD;
hh.a[][]=***;
hh.a[][]=**;
hh.a[][]=*;
hh.a[][]=;
hh.a[][]=;
}
int main()
{
init();
int T,cas=;
scanf("%d",&T);
while(T--)
{
ll n,a,b;
scanf("%lld%lld%lld",&n,&a,&b);
init1(a,b);
Matrix ans=(base^(n-));
hh=hh*ans;
printf("%lld\n",hh.a[][]);
}
return ;
}

hdu 5950 Recursive sequence 矩阵快速幂的更多相关文章

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

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

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

  3. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

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

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

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

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

  6. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  7. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  8. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

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

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

随机推荐

  1. Atom Remote-FTP connecting FTP with SSL/TLS

    { "protocol": "ftp", "host": "xxx.xxx.xxx.xxx", "port&q ...

  2. JQUERY解析XML IE8的兼容问题

    var str="xml字符串"; alert($(str).find("Row").attr("Id")); 在IE8下,这段脚本无法运行 ...

  3. org.hibernate.MappingException: Unknown entity常见问题。回顾笔记,以前没记,现在补上,xiaochao写的蛮好的直接给转载了。

    转自http://www.blogjava.net/xiaochao/articles/hibernatetopic.html. 官方说明如下: Hibernate遵循EJB3.0实体bean的注解规 ...

  4. JavaEE基础(十四) /正则

    1.常见对象(正则表达式的概述和简单使用) A:正则表达式 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. 作用:比如注册邮箱,邮箱有用户名和 ...

  5. Python字符界面函数库

    curses ncurses prettytable from prettytable import PrettyTable row = PrettyTable() row.field_names = ...

  6. HDU 4453:Looploop(Splay各种操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=4453 题意:很多种操作:1.add x,将从光标起的 k2 个数全部加上 x:2.reverse,将从光标起的 ...

  7. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  8. GOICE项目初探

    GOICE项目初探         在图像拼接方面,市面上能够找到的软件中,要数MS的ICE效果.鲁棒性最好,而且界面也很美观.应该说有很多值得学习的地方,虽然这个项目不开源,但是利用现有的资料,也可 ...

  9. python代码

    f=open('/home/xbwang/newtextsimilarity/data/glove/glove.840B.300d.txt','r') count = 0 print ' '.join ...

  10. JavaScript的构造器与对象(二)

    constructor 的用法:对象的构造函数  每一个函数的Prototype属性指向的对象都包含唯一一个不可枚举属性constructor,该属性的值是这么一个对象:它指向了它所在的构造函数. 语 ...