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\\ ...
随机推荐
- fflush函数的深入理解
本人昵称sky,欢迎与各位多多交流学习 这样的c程序想必大家都不陌生,fflush()这个函数有清除输入输出缓存的功能,那很多人就会问了,什么是清除输入输出缓存呢? 其实就是我们在printf输出的时 ...
- ModalDialog.js
1. add <base target="_self" /> in the page of dialog, no need to use frame: <head ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- Lucas定理模板
用于大组合数对p取模的计算. #include <cstdio> #include <iostream> #include <cmath> #include < ...
- HDU 5794:A Simple Chess(Lucas + DP)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里 ...
- 用C#.NET编写软件注册机
验证注册码是保护软件产品产权的常用手段.一般过程如下, 1. 软件发行者收集用户特有的信息: 2. 根据用户特有的信息,使用注册机生成注册码并把注册码发给客户: 3. 向软件导入注册码,由软件自 ...
- LeetCode----67. Add Binary(java)
package addBinary67;/* Given two binary strings, return their sum (also a binary string).For example ...
- 减少GC开销的措施
程序的运行会直接影响系统环境的变化,从而影响GC的触发.若不针对GC的特点进行设计和编码,就会出现内存驻留等一系列负面影响.为了避免这些影响,基本的原则就是尽可能地减少垃圾和减少GC过程中的开销.具体 ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 第十一章 Android 内核驱动——Alarm
11.1 基本原理 Alarm 闹钟是 android 系统中在标准 RTC 驱动上开发的一个新的驱动,提供了一个定时器 用于把设备从睡眠状态唤醒,当然因为它是依赖 RTC 驱动的,所以它同时还可以 ...