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\\ ...
随机推荐
- linux设备驱动归纳总结(十一):写个简单的看门狗驱动【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-112879.html linux设备驱动归纳总结(十一):写个简单的看门狗驱动 xxxxxxxxxxx ...
- 一篇文章一张思维导图看懂Android学习最佳路线
一篇文章一张思维导图看懂Android学习最佳路线 先上一张android开发知识点学习路线图思维导图 Android学习路线从4个阶段来对Android的学习过程做一个全面的分析:Android初级 ...
- java 使用反射技术解耦
1.调用的代码 /src/de/test.java package de; public class Test { public static void main(String[] args) { D ...
- ant 使用指南---java模块化编译【转】
转自:http://www.cnblogs.com/hoojo/archive/2013/06/14/java_ant_project_target_task_run.html 一.概述 ant 是一 ...
- HDU 1890:Robotic Sort(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的 ...
- Effective C++第三遍
试图调用private的copy或赋值函数是编译期错误,而调用没有具体定义的函数则是连接期错误. 以对象管理资源:智能指针RAII(资源获取立即初始化)后都是对象,但有时候,比如(API的)函数参数要 ...
- java中+的使用
在java中+可以做为连接符和运算符两种使用方法 例如: 代码: int X=100; int Y=200; System.out.println("X+Y=" + (X + ...
- Android关机闹钟实现
Android关机闹钟实现 时间转换网站:http://tool.chinaz.com/Tools/unixtime.aspx 1.apk层 这个还是比较简单的,百度一下就可以看到apk的代码,我之前 ...
- 使用jquery再次封装ajax
$.fn.ajaxSend = function (type, url, postdata, onSuccess) { $.ajax({ async: false, url: url, type: t ...
- EasyUI DataGrid 复选框
使用checkbox,用户可以选定/取消数据行.添加checkbox列,我们简单的添加列的checkbox属性,并且设置为true.代码像这样:<table id="tt"& ...