Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30    Accepted Submission(s): 20

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
题意:f[i]=2*f[i-2]+f[i-1]+i^4
题解:[f[i],f[i-1],i^4,i^3,i^2,i,1]
  1 1 0 0 0 0 0
  2 0 0 0 0 0 0
  1 0 1 0 0 0 0
  4 0 4 1 0 0 0
  6 0 6 3 1 0 0
  4 0 4 3 2 1 0
  1 0 1 1 1 1 1
构造矩阵  矩阵快速幂
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
const ll k=;
struct matrix
{
ll m[][];
} ans,exm;
struct matrix matrix_mulit1(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
there.m[i][j]=;
for(int u=;u<;u++)
there.m[i][j]=(there.m[i][j]+aa.m[i][u]*bb.m[u][j]%k)%k;
}
}
return there;
}
struct matrix matrix_mulit2(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int j=;j<;j++)
{
there.m[][j]=;
for(int u=;u<;u++)
there.m[][j]=(there.m[][j]+aa.m[][u]*bb.m[u][j]%k)%k;
}
return there;
}
ll matrix_quick(ll aa,ll bb,ll gg)
{
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
ans.m[][]=bb;ans.m[][]=aa;ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;
gg-=;
while(gg)
{
if(gg&)
ans=matrix_mulit2(ans,exm);
exm=matrix_mulit1(exm,exm);
gg >>= ;
}
return ans.m[][];
}
int t;
ll n,a,b;
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%I64d %I64d %I64d",&n,&a,&b);
if(n==)
printf("%I64d\n",a);
else if(n==)
printf("%I64d\n",b);
else
printf("%I64d\n",(matrix_quick(a, b, n)+k)%k);
}
return ;
}

HDU 5950 矩阵快速幂的更多相关文章

  1. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  2. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  3. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  4. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  5. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  6. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  7. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  8. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

  9. HDU 4686 矩阵快速幂 Arc of Dream

    由式子的性质发现都是线性的,考虑构造矩阵,先有式子,a[i] = ax * a[i-1] + ay; b[i] = bx*b[i-1] +by; a[i]*b[i] = ax*bx*a[i-1]*b[ ...

随机推荐

  1. Linux命令基本格式及目录处理命令

    命令提示符 [root@localhost ~]# root:当前登录用户 localhost:主机名 ~:当前所在的目录,此处为"家"目录 #:root超级用户的提示符,如果是普 ...

  2. vs2012编译Qwt

    主题:vs2012编译Qwt ------------------------------------------------------------------------- 参照网络地址: htt ...

  3. ITerm2下使用ssh访问Linux

    通常情况下,iTerm2访问远程Linux使用ssh,与Termial基本一样,方法如下: ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口 ...

  4. Js 类定义的几种方式

    提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...

  5. Maven 工程错误Failure to transfer org.codehaus.plexus:plexus-io:pom:1.0,Failure to transfer org.codehaus.plexus:plexus-archiver:jar:2.0.1

    原本好好的Maven工程却出现了莫名的错误 Failure to transfer org.codehaus.plexus:plexus-archiver:jar:2.0.1 from http:// ...

  6. AngularJS事件绑定的使用详解

    本文和大家分享的主要是AngularJS中事件绑定相关知识点,希望通过本文的分享,对大家学习和使用AngularJS有所帮助. 1.绑定事件:表达式.事件方法名: 2.绑定点击事件实例:显示.隐藏页面 ...

  7. redis 持久化 如果 AOF 文件出错了,怎么办?

    服务器可能在程序正在对 AOF 文件进行写入时停机, 如果停机造成了 AOF 文件出错(corrupt), 那么 Redis 在重启时会拒绝载入这个 AOF 文件, 从而确保数据的一致性不会被破坏. ...

  8. Samba Linux 和windows 共享

    1.安装Samba  (yum install Samba) 2.配置Samba  (Samba的配置文件为/etc/samba/smb.conf) 1)打开smb.conf   vim /etc/s ...

  9. 初学JAVA

    通过eclipse开发java程序:1.创建一个java项目 2.创建程序包 3.编写java源程序 4.运行java程序 例如开发"helloworld "java程序: 1.进 ...

  10. linux文件复制与权限赋值

    文件file内容赋值到file2中 1. cp  file1  file2 2. cat file1 > file2 3. cat < file1 > file2 4. dd if= ...