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 ...
随机推荐
- Centos 7.4 源码 Nginx 安装
一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.首先要安装 PCRE ...
- Node.js编写be的流程(express)
Node.js编写be的流程 1.当前项目目录下首先安装express 2.自动生成express插件结构 express -e 3.执行完前两步的效果 4.此时的package.json ...
- 最短路径:Dijkstra & Floyd 算法图解,c++描述
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 从mysql读取数据写入mongo
# coding:utf-8 # Created by qinlin.liu at 2017/3/14 import pymysql import datetime #pymongo说明文档 : h ...
- DevExpress WinForms v18.2新版亮点(三)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...
- win 10 初始环境变量
有时用户会修改Win10系统的环境变量,改到后面原来是什么的也记不得了,想要改回去还要去别的电脑查看,这里转载下Win10 64位环境变量的默认初始值. 附:打开环境变量方法:电脑左下右键——系统—— ...
- 监控中的TP50
TP指标: TP50:指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第50%的那个值作为TP50 值:配置此监控指标对应的报警阀值后,需要保证在 ...
- 安装连接mysql8时候遇到的问题以及解决(转)
官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...
- 国行 lg g3 D858 刷 lg g3 D858hk 教程(备忘)
纯手打,转载请注明出处~ 刷机有风险,出现问题概不负责! 本着自娱自乐的宗旨 ,分享一下,出了问题不负责! 准备的材料: 1,手机一枚(废话)国行lg g3 d858 2,root 工具 用来root ...
- 201621123001 《Java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. Mysql数据库简单操作,常用的操作命令 启动:进入Mysql (从命令行mysql -u root -p) 退 ...