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 ...
随机推荐
- spring cloud jwt用户鉴权及服务鉴权
用户鉴权 客户端请求服务时,根据提交的token获取用户信息,看是否有用户信息及用户信息是否正确 服务鉴权 微服务中,一般有多个服务,服务与服务之间相互调用时,有的服务接口比较敏感,比如资金服务,不允 ...
- py propterties reuqest.post
import tracebackclass Properties(object): def __init__(self, fileName): self.fileName = fileName sel ...
- 【Loadrunner_Http接口】使用Loadrunner对天气信息的接口编写脚本
方法一:使用get请求 Action() { //http接口访问,get请求 web_url("www.abc.com", "URL=http://v.juhe.cn/ ...
- laravel中的plicy授权方法:
1.用命令新建policy: php artisan make:policy PostPolicy 2.在app/Policies/PostPolicy.php中添加处理文件的权限的方法: //修改: ...
- python2和python3的区别总结
python2x和python3x区别: python2x:源码重复,不规范. python3x: 源码规范,优美,清晰,简单. 编译型:将代码一次性全部转化成字节码. 代表语言:C,C++ 优点: ...
- jenkins部署java项目,脚本文件放在远程仓库中 和jar一起打包(六)
jenkins部署java项目到远程linux上,脚本文件和项目一起上传到gogs上,直接执行gogs上的脚本文件来执行项目 (1)新建maven项目 pom.xml的配置 <project x ...
- js 动态绑定鼠标事件
<script> function getElementsByClassName(n) { var classElements = [],allElements = document.ge ...
- Java反射(一眼就看会)
参考:(1)http://blog.csdn.net/liujiahan629629/article/details/18013523(2)https://www.zhihu.com/question ...
- set循环遍历删除特定元素
使用Iterator迭代器 public class Demo { public static void main(String[] args) { Set<Object> obj = n ...
- 什么是XP
极限编程(XP)是敏捷过程中最富盛名的一个.下述这些特点使得敏捷过程能够较好地适应商业竞争环境下对小型项目提出的有效资源和有限开发时间的约束. 极限编程的有效实践 极限编程的整体开发过程 极限编程的迭 ...