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. mac os 添加用户到组 命令

    mac os 添加用户到组 命令 dscl localhost 进入组目录 cd /Local/Default/Groups 使用ls 你就可以看到所有的group,? /Local/Default/ ...

  2. git学习笔记总结

    git试免费的开源的分布式版本控制系统,github是一个用git做版本控制的项目托管平台.说白了git就是帮忙你管理你开发的代码,代码每次修改的历史,多人更好的一起开发项目. 分布式版本控制系统,每 ...

  3. LinkedList源码分析

    LinkedList也和ArrayList一样实现了List接口,但是它执行插入和删除操作时比ArrayList更加高效,因为它是基于链表的.基于链表也决定了它在随机访问方面要比ArrayList逊色 ...

  4. IAR更改代码字体&快速模板设置。——Arvin

    1.是用软件提供的字体 如果只想简单的设置,可进行如下设置Tools->IDE Options->Editor->Colors and Fonts->Editor Font-& ...

  5. Apriori原理与实践

    Apriori: 其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集.经典的关联规则数据挖掘算法Apriori 算法广泛应用于各种领域,通过对数据的关联性进行了分析和挖掘,挖掘出的这 ...

  6. JreeeChart入门

    JFreeChart主要用来各种各样的图表,这些图表包括:饼图.柱状图(普通柱状图以及堆栈柱状图).线图.区域图.分布图.混合图.甘特图以及一些仪表盘等等 (源代码下载) 示例程序运用的jar包: j ...

  7. postgres 查询数据库所有表名

    select relname as TABLE_NAME ,col_description(c.oid, 0) as COMMENTS from pg_class cwhere relkind = ' ...

  8. public,protected,private辨析

    一直没有很清楚理解这三个修饰权限的区别,今天终于搞明白了,现总结如下: private:最严格的一个,子类无法继承,只有本类内部内访问,在其余类及子类中通过 "类名.方法" 去调用 ...

  9. Python高效编程的19个技巧

    初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...

  10. CodeForces #367 div2 C

    题目链接: Hard problem 题意:每个字符串可以选择反转或者不反转,给出反转每个字符串的代价,问使最少的代价使得这个字符串序列成字典序. dp[i][j] = x : 第一维是第i个字符串, ...