HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门
题目:
Recursive sequence
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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-)-th number, the (i-)-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 < 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 . Sample Input Sample Output Hint
In the first case, the third number is = *1十2十3^.
In the second case, the third number is = *1十1*10十3^ and the fourth number is = * 十 十 ^.
题目大意:
已知a1 = a,a2 = b,ai = ai-1 + 2ai-2 + n4,求aN,答案对2147493647取模。
N,a,b < 231。
思路:
因为N超级大,考虑快速幂,但是通项怎么都搞不出来。
又题目给的是递推式,考虑用矩阵快速幂。。。。
令Fn = [an-1, an],则Fn-1 = [an-2,an-1],但是这样an+1就算不上n4了。那就把n4加上去试试看:
再令Fn = [an-1,an,(n+1)4],这样就可以推出an+1了,但是(n+1)4又不能递推。。。展开(n+1)4发现:(n+1)4 = n4 + 4n3 + 6n2 + 4n + 1,可以由n4、n3、n2、n、1递推出来。同时(n+1)3、(n+1)2、n+1、1都可以用n4、n3、n2、n、1递推出来,所以Fn和系数矩阵base就出来了:
Fn = [an-1,an,(n+1)4,(n+1)3,(n+1)2,n+1,1];
$\begin{bmatrix}
0 &2 &0 &0 &0 &0 &0 \\
1 &1 &0 &0 &0 &0 &0 \\
0 &1 &1 &0 &0 &0 &0 \\
0 &0 &4 &1 &0 &0 &0 \\
0 &0 &6 &3 &1 &0 &0 \\
0 &0 &4 &3 &2 &1 &0 \\
0 &0 &1 &1 &1 &1 &1
\end{bmatrix}$
代码:
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
#define MAXN 7
const ll mod = ; struct Matrix
{
ll mat[MAXN][MAXN];
Matrix() {}
Matrix operator*(Matrix const &b)const
{
Matrix res;
memset(res.mat, , sizeof(res.mat));
for (int i = ;i < MAXN; i++)
for (int j = ; j < MAXN; j++)
for (int k = ; k < MAXN; k++)
res.mat[i][j] = (res.mat[i][j]+this->mat[i][k] * b.mat[k][j])%mod;
return res;
}
}base;
Matrix pow_mod(Matrix base, ll n)
{
Matrix res;
memset(res.mat, , sizeof(res.mat));
for (int i = ; i < MAXN; i++)
res.mat[i][i] = ;
while (n > )
{
if (n & ) res = res*base;
base = base*base;
n >>= ;
}
return res;
}//输入基础矩阵返回n次幂的矩阵;
//res.mat[0][0] 就是最终的值F(N+1)
//注意修改MAXN和mod void init() {
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
} int main()
{
init();
int T;
cin >> T;
while (T--) {
ll n, a, b;
scanf("%lld%lld%lld", &n, &a, &b);
Matrix F2;
memset(F2.mat, , sizeof F2.mat);
F2.mat[][] = a; F2.mat[][] = b; F2.mat[][] = ***; F2.mat[][] = **; F2.mat[][] = *; F2.mat[][] = ; F2.mat[][] = ;
Matrix FN = F2*pow_mod(base, n-);
cout << FN.mat[][] << endl;
}
return ;
}
HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)的更多相关文章
- 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 ...
- CH 3401 - 石头游戏 - [矩阵快速幂加速递推]
题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...
- HDU 1757 矩阵快速幂加速递推
题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- 洛谷P1357 花园(状态压缩 + 矩阵快速幂加速递推)
题目链接:传送门 题目: 题目描述 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(<=N<=^).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻 ...
- [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- CH3401 石头游戏(矩阵快速幂加速递推)
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
- POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】
题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
随机推荐
- 码云git使用一(上传本地项目到码云git服务器上)
主要讲下如果将项目部署到码云git服务器上,然后使用studio导入git项目,修改本地代码后,并同步到码云git上面. 首先:我们在码云上注册账号并登陆.官网(https://git.oschina ...
- 学习笔记-AngularJs(二)
在接下来学习angularjs中,我按照的就是之前 学习笔记-AngularJs(一)所讲的目录来搭建一个学习的项目,做一个互联网大佬人物简介的例子,当然也可以使用angualrjs上面提供的官方例子 ...
- nyoj-0708-ones(dp)
nyoj-0708-ones 题意:用1,+,*,(,). 这四个符号组成表达式表达数s(0 <= s <= 10000),且1最少时1的的个数 状态转移方程: dp[i] = min(d ...
- 计算机基础part1
一:计算机的基本组成 1.计算机由输入单元.控制单元.算法逻辑单元.输出单元.存储单元,五大单元组成 二:概念篇 CPU:中央处理器,其内含有指令集(取码-解码-执行的过程) CPU同一时刻只能干一件 ...
- BCM5396的SPI理解
参考文档链接:https://pan.baidu.com/s/1kuXJmULwtjOW1TeOuTRPQQ *时钟极性和相位 BCM538X / BCM5396用于根据以下标准发送/接收SPI数据: ...
- CAN总线(1)--初探(更新中)
前言: CAN总线可以控制可以使用Xilinx中IP核来直接实现,也可以使用专用的CAN芯片(例如:SJA1000)通过单片机和FPGA驱动控制来实现: 目前是使用控制器SJA1000来进行实现: C ...
- JavaWeb基础-Session和Cookie
JSP状态管理 http的无状态性,服务器不会记得发送请求的浏览器是哪一个 保存用户状态的两大机制:session和cookie Cookie:是web服务器保存在客户端的一系列文本信息 作用:对特定 ...
- capjoint conversations with Chenweiwen
This event is quite small for teleseismic stations, which means it will be more strongly affected by ...
- 强化学习3-蒙特卡罗MC
之前讲到强化学习可以用马尔科夫决策过程来描述,通常情况下,马尔科夫需要知道 {S A P R γ},γ是衰减因子,那为什么还需要蒙特卡罗呢? 首先什么是蒙特卡罗? 蒙特卡罗实际上是一座赌城的名字,蒙 ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...