hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
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.
3 1 2
4 1 10
369
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.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,MOD=;
struct Matrix
{
ll a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+(a[i][k]*B.a[k][j])%MOD)%MOD;
return C;
}
Matrix operator ^ (const ll &t)const
{
Matrix A=(*this),res;
res.init();
int p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
Matrix base,hh;
void init()
{
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
}
void init1(ll a,ll b)
{
memset(hh.a,,sizeof(hh.a));
hh.a[][]=b%MOD;
hh.a[][]=a%MOD;
hh.a[][]=***;
hh.a[][]=**;
hh.a[][]=*;
hh.a[][]=;
hh.a[][]=;
}
int main()
{
init();
int T,cas=;
scanf("%d",&T);
while(T--)
{
ll n,a,b;
scanf("%lld%lld%lld",&n,&a,&b);
init1(a,b);
Matrix ans=(base^(n-));
hh=hh*ans;
printf("%lld\n",hh.a[][]);
}
return ;
}
hdu 5950 Recursive sequence 矩阵快速幂的更多相关文章
- 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 ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
随机推荐
- 快速稳定的维护PHP
Just to recap, previously we'd have this sort of thing: namespace me\adamcameron\testApp; use Guzzle ...
- coco2dx服务器简单例子
实现的单个socket例子,了解socket原理. 先创建一个win32的项目(命令行的),作为服务端 // SocketServer.cpp : 定义控制台应用程序的入口点.// #include ...
- JavaScript DOM 编程艺术(第2版)读书笔记 (8)
<!--这章的内容略奇怪啊!可能是因为我之前没有接触过这些知识点,等以后用到的时候再回来翻阅吧,现在先简要介绍一下 js权限已通过,博客园好快的效率啊,谢谢O(∩_∩)O --> 缩略语 ...
- android 代码整体回退
repo forall -c 'HAHA=`git log --before="3 days" -1 --pretty=format:"%H"`;git res ...
- Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8117 Accepted: 1883 Desc ...
- 2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛--n a^o7 ! 分类: 比赛 2015-06-09 17:16 14人阅读 评论(0) 收藏
n a^o7 ! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 All brave and intelligent fighte ...
- DataRow数组转换DataTable
public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...
- 使用oracle写if判断
DECLARE L_X INT; BEGIN SELECT COUNT(*) INTO L_X FROM SYSTEMROLEFUNCTION WHERE ROLEID = '1'; IF L_X & ...
- 网页撤销后ubuntu本地撤销
git reset --hard <commit_id> 回到上一个commit: 之后再 git reset --hard :
- Acdream Chinese Girls' Amusement
A - Chinese Girls' Amusement Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Jav ...