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 ...
随机推荐
- [转]JVM内存模型
最近排查一个线上java服务常驻内存异常高的问题,大概现象是:java堆Xmx配置了8G,但运行一段时间后常驻内存RES从5G逐渐增长到13G #补图#,导致机器开始swap从而服务整体变慢.由于Xm ...
- sublime text 3 笔记 简单配置
一.首先我们去sublime text 3 去下载(http://www.sublimetext.com/) 下载完成后,打开页面 二.汉化 1.如图所示,点击菜单栏中“preferences”,弹出 ...
- Notes on Large-scale Video Classification with Convolutional Neural Networks
Use bigger datasets for CNN in hope of better performance. A new data set for sports video classific ...
- javascript 多个onclick function 取对应值
方法1: 直接获取值 <button onclick="aa(1)">执行</button> <button onclick="aa(2)& ...
- C/S与B/S架构对比
概述 在这个信息急剧膨胀的社会,我们不得不说人类正进入一个崭新的时代,那就是信息时代.信息时代的一个主要而显著的特征就是计算机网络的应用.计算机网络从最初的集中式计算,经过了Client/Server ...
- [BZOJ1588]营业额统计
Problem 每次给你一个数,找出前面的数与这个数的差的绝对值的最小值 Solution Splay Notice 找不到前驱和后继时,会出错. Code #include<cmath> ...
- CSS(一)属性--border边框
HTML代码 <body> <div>举个例子</div> </body> CSS代码: div{ font-size:12px; //字体大小,默认 ...
- 从mysql读取数据写入mongo
# coding:utf-8 # Created by qinlin.liu at 2017/3/14 import pymysql import datetime #pymongo说明文档 : h ...
- 如何在Ubuntu中安装中文输入法
在使用ubuntu系统时,有的时候总觉得英文输入法不方便操作,总希望能有中文输入法可以辅助操作,那怎样才能在ubuntu中安装中文输入法呢?下面有一种简单的方法可以安装中文输入法. 如何在ubuntu ...
- VSTO:使用C#开发Excel、Word【5】
<Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath >——By Eric C ...