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. jquery之remove(),detach()方法详解

    一:remove()方法 remove()函数用于从文档中移除匹配的元素. 你还可以使用选择器进一步缩小移除的范围,只移除当前匹配元素中符合指定选择器的部分元素. 与detach()相比,remove ...

  2. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

  3. SpringMVC控制器与视图的数据交换

    1,先创建spring的主配置文件(applicationContaxt.xml如果写在WEB-INF下,就不用配置context了,就是不用告诉它路径了,WEB-INF会自动加载的),由监听器负责加 ...

  4. ios下移除原生样式

    之前遇到过这个问题,今天无意中在网上看见博文 记录一下. 设置这个就好: -webkit-appearance : none ;

  5. Java开发高薪之路__大纲篇

    本人小白,现对java开发做出以下总结,内容将从初级开始,逐步完善与摸索. 基础篇 网页篇 Android篇 高级建设篇 数据篇 系统篇

  6. html 通用 遮罩弹出层 弹出后 支持跳转页面

    //showMessage 提示的内容默认为空必填 buttonText:按钮显示的内容默认为"确定" 传入 "" 为默认 url:跳转链接 传入"& ...

  7. SPSS数据分析—多重线性回归

    只有一个自变量和因变量的线性回归称为简单线性回归,但是实际上,这样单纯的关系在现实世界中几乎不存在,万事万物都是互相联系的,一个问题的产生必定多种因素共同作用的结果. 对于有多个自变量和一个因变量的线 ...

  8. Hibernate 测试分析 好多题都是没认真看题错了。

    此题目考查的是对Hibernate中交叉连接的理解.HQL支持SQL风格的交叉连接查询,交叉连接适用于两个类之间没有定义任何关联时.在where字句中,通过属性作为筛选条件,如统计报表数据.使用交叉连 ...

  9. Android 《第一行代码》 第二章练习代码 ActivityTest

    FirstActivity.java package com.example.activitytest; import android.app.Activity; import android.con ...

  10. iOS开发Swift篇—(三)字符串和数据类型

    iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http://www ...