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. SQL Prompt自定义代码片段

    新增代码片段: 代码片段管理: 代码片段中可以使用以下占位符:详见参考: $DATE$ 插入当前日期. $TIME$ 插入当前时间 $USER$ 插入当前电脑的用户名 $PASTE$ 插入剪切板内容 ...

  2. jquery之replaceAll(),replaceWith()方法详解

    一:replaceAll() replaceAll()函数用于使用当前匹配元素替换掉所有的目标元素. 该函数属于jQuery对象(实例). 语法 jQuery 1.2 新增该函数. jQueryObj ...

  3. ORACLE分页查询SQL语法——最高效的分页

    --1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT  ...

  4. java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51

    http://blog.csdn.net/e_wsq/article/details/52100234 一日换了一下MyEclipse,换成2016CI,结果从SVN上下载了一个工程后出现以下错误: ...

  5. Android之打log

    Android之打log 1.在代码中加上自己的log 2,模块编译mm -B或者./mk mm/mmm packages/apps/Contacts/ 3编译成功后install或者push生成的a ...

  6. js String对象中常用方法小结(字符串操作)

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  7. Spirng中Mongodb中write-concern的解释

    Spring在插入数据库时没有返回状态配置:write-concern获取异常<beans xmlns="http://www.springframework.org/schema/b ...

  8. SpringMVC -- 梗概--壹

    1.springMVC:MVC开源框架 2.springMVC开发流程: 2.1 导包: 2.2 配置前端控制器(核心) DispatcherServlet <servlet> <s ...

  9. [maven] maven变量

    Maven内置变量说明: $${project.basedir}或{basedir} 项目根目录,即包含pom.xml文件的目录 ${project.version}或${version}表示项目版本 ...

  10. progresql - 常用的管理命令

    1.查看当前数据库实例的版本 Select version(); 2.查看数据库的启动时间 Select pg_postmaster_start_time(); 3.查看最后load配置文件的时间 s ...