矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题意:

思路:
直接拿别人的图,自己写太麻烦了~

然后就可以用矩阵快速幂套模板求递推式啦~
另外:
这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Google Codejam Round 1A的C题。
#include <bits/stdc++.h> typedef long long ll;
const int N = 5;
int a, b, n, mod;
/*
*矩阵快速幂处理线性递推关系f(n)=a1f(n-1)+a2f(n-2)+...+adf(n-d)
*/
struct Matrix {
int row, col;
ll arr[N][N];
Matrix(int r=0, int c=0) {
row = r; col = c;
memset (arr, 0, sizeof (arr));
}
Matrix operator * (const Matrix &B) {
Matrix ret(row, B.col);
for (int i=0; i<row; ++i) {
for (int j=0; j<B.col; ++j) {
for (int k=0; k<col; ++k) {
ret.arr[i][j] = (ret.arr[i][j] + (ll) arr[i][k] * B.arr[k][j]) % mod;
}
}
}
return ret;
}
void unit(int n) {
row = col = n;
for (int i=0; i<n; ++i) {
arr[i][i] = 1;
}
}
};
Matrix operator ^ (Matrix X, ll n) {
Matrix ret; ret.unit (X.col);
while (n) {
if (n & 1) {
ret = ret * X;
}
X = X * X;
n >>= 1;
}
return ret;
} int f[3], x[3]; int main() {
while (scanf ("%d%d%d%d", &a, &b, &n, &mod) == 4) {
double c = (double) a + sqrt ((double) b);
f[1] = ((ll) ceil (c)) % mod;
f[2] = ((ll) ceil (c*c)) % mod;
int d = 2;
x[1] = (2*a) % mod; x[2] = (-(a*a-b) % mod + mod) % mod; if (n <= d) {
printf ("%d\n", f[n]);
} else {
Matrix Fn(d+1, d+1), Fd(d+1, 1);
for (int i=0; i<Fn.row-1; ++i) {
Fn.arr[i][i+1] = 1;
}
for (int i=1; i<Fn.col; ++i) {
Fn.arr[Fn.row-1][i] = x[d-i+1];
}
for (int i=0; i<Fd.row; ++i) {
Fd.arr[i][0] = f[i];
}
Fn = Fn ^ (n - d);
Fn = Fn * Fd;
printf ("%d\n", Fn.arr[d][0]);
}
}
return 0;
}
矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)的更多相关文章
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- 循环节 + 矩阵快速幂 - HDU 4291 A Short problem
A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...
- 矩阵快速幂--HDU 6030 Happy Necklace
Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single string ...
- HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )
链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
[解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...
- hdu 4565 So Easy!(矩阵+快速幂)
题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
随机推荐
- Linux快速上手
1.Linux系统架构 内核(kernel) 内存管理(mm) Linux内存特性无论物理内存有多大,Linux 都将其充份利用,将一些程序调用过的硬盘数据读入内存,利用内存读写的高速特性来提高Lin ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- STM32F10xxx 之 System tick Timer(SYSTICK Timer)
背景 研究STM32F10xxx定时器的时候,无意间看到了System tick Timer,于是比较深入的了解下,在此做个记录. 正文 System tick Timer是Cotex-M内核的24位 ...
- AVAudioPlayer
AVAudioPlayer在AVFoundation框架下,所以我们要导入AVFoundation.framework. AVAudioPlayer类封装了播放单个声音的能力.播放器可以用NSURL或 ...
- React Native踩坑Tip
最近在使用React Native(以下简称RN)中踩了个坑,RN只能异步调用原生方法,所以在原生方法直接调用UI刷新操作需要将任务递交到主线程才可以. RCT_EXPORT_METHOD(finis ...
- the operation was attempted on an empty geometry Arcgis Project异常
处理gis数据,投影变换时出现异常: the operation was attempted on an empty geometry 解决思路: arcgis的repair geometry方法:删 ...
- canvas九宫格跑马灯
canvas九宫格跑马灯抽奖 之前用dom写了一版,部分 安卓机会卡顿,换用canvas dom版本九宫格抽奖
- js 控制文本只能输入数字
代码如下: <input onkeypress="setNumber()"><script> function setNumber(){ var keyCo ...
- yum 保存下载包
--- 1 --- $ sudo yum install yum-plugin-downloadonly $ sudo yum install --downloadonly --downloaddir ...
- Mac Pro 8G 安装MyEclipse提示虚拟内存为0 安装失败
看的一个大神的博客,一句话解决了.哈哈 百度一下很多人都说开多一点程序,让程序占满内存,使其虚拟内存使用就能通过这一步骤,但这里有个更好一点的方案 通过执行: memory_pressure -l ...