Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2882    Accepted Submission(s): 1284

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.

 
题意     f[n]=f[n-1]+2*f[n-2]+n^4; f[1]=a f[2]=b   求第n项
解析     直接递推肯定会超时的   所以 构造一个7*7系数矩阵 直接快速幂解出来  由于现在比较菜 只会最简单的矩阵 勉强可以写出来。。。。
 
1 2 1 0 0 0 0         f[i-1]        f[i]
1 0 0 0 0 0 0         f[i-2]        f[i-1]  
0 0 1 4 6 4 1    i^4          (i+1)^4
0 0 0 1 3 3 1       *    i^3                  =            (i+1)^3
0 0 0 0 1 2 1    i^2          (i+1)^2
0 0 0 0 0 1 1    i            i+1
0 0 0 0 0 0 1      1           1  
 
AC代码
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=,maxn=;
struct Matrix
{
ll m[maxn][maxn];
Matrix()
{
memset(m,,sizeof(m));
}
void init()
{
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
m[i][j]=(i==j);
}
Matrix operator +(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
c.m[i][j]=(m[i][j]+b.m[i][j])%mod;
}
}
return c;
}
Matrix operator *(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
for(int k=; k<maxn; k++)
{
c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%mod)%mod;
}
}
}
return c;
}
Matrix operator^(const ll &t)const
{
Matrix ans,a=(*this);
ans.init();
ll n=t;
while(n)
{
if(n&) ans=ans*a;
a=a*a;
n>>=;
}
return ans;
}
};
int main()
{
int t;
ll n,m,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld %lld",&n,&a,&b);
if(n==)
{
cout<<a%mod<<endl;
continue;
}
if(n==)
{
cout<<b%mod<<endl;
continue;
}
Matrix temp;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
Matrix aa,bb;
bb.m[][]=b%mod;
bb.m[][]=a%mod;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
aa=temp^(n-);
aa=aa*bb;
cout<<aa.m[][]%mod<<endl;
}
return ;
}
    

HDU 5950 Recursive sequence 递推转矩阵的更多相关文章

  1. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  2. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  3. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

  5. HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  6. HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)

    Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  7. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  8. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  9. HDU 5950 Recursive sequence(矩阵快速幂)题解

    思路:一开始不会n^4的推导,原来是要找n和n-1的关系,这道题的MOD是long long 的,矩阵具体如下所示 最近自己总是很坑啊,代码都瞎吉坝写,一个long long的输入写成%d一直判我TL ...

随机推荐

  1. CSS综合用法

    div 居中 {position: absolute; top: 50%; left: 50%; margin-top: -180px; margin-left: -160px;}

  2. Objective -C Object initialization 对象初始化

    Objective -C Object initialization 对象初始化 1.1 Allocating Objects  分配对象 Allocation is the process by w ...

  3. Vue 拦截器的使用

    拦截器 可以全局进行拦截器设置.拦截器在发送请求前或响应返回时做一些特殊的处理. 拦截器的注册 Vue.http.interceptors.push({ request: function ( req ...

  4. xamarin 学习笔记02- IOS Simulator for windows 安装

    微软发布了在window下的ios模拟器 下载 ios模拟器 并安装在windows系统上. Xamarin for Visual Studio 和 网络上的 Mac 中的 Xamarin.iOS 开 ...

  5. 【Gambit】Gambit使用教程

    第一章 Gambit使用 Gambit介绍 网格的划分使用Gambit软件,首先要启动Gambit,在Dos下输入Gambit <filemane>,文件名如果已经存在,要加上参数-old ...

  6. org.springframework.orm.hibernate4.support.OpenSessionInterceptor

    /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Vers ...

  7. 理解 call, apply 的用法

    callcall() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数. function list() { return Array.prototype.slice.call ...

  8. laravel如何自定义控制器目录

    默认控制器在App\Http\Controllers目录下,如何自定义目录呢? 首先我们看一下laravel的请求周期 我们看一下服务提供者RouteServicePrivder文件中的一个函数 /* ...

  9. 使用GetLogicalDrives获取卷标

    #include<stdio.h> #include<windows.h> int main() { DWORD dwLogical= GetLogicalDrives(); ...

  10. laravel JWTAuth实现api接口鉴权(基础篇)

    官网:https://jwt-auth.readthedocs.io 参考:https://learnku.com/articles/10885/full-use-of-jwt#99529f 1.to ...