hdu4565---So Easy!(矩阵)
来源:互联网 发布:知乎vs知道 编辑:程序博客网 时间:2024/06/04 08:49
Problem Description
A sequence Sn is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
For each the case, output an integer Sn.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you: 5185 5184 5181 5180 5177
(a+sqrt(b))^n最后的形式一定形如
X+Y * sqrt(b)
n范围很大,需要用矩阵来加速
推出转移矩阵为
a 1
b a
(a+sqrt(b))^n = X+Y * sqrt(b)
(a-sqrt(b))^n = X-Y * sqrt(b)
a - 1< sqrt(b) < a
所以z = (a-sqrt(b))^n 大于0 小于1
设ans = (a+sqrt(b))^n
ans+z = 2 * X
2 * X - 1 < ans = 2 * X - z < 2 * X
那么向上取整就是2 * X
/************************************************************************* > File Name: hdu4565.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年03月14日 星期六 11时01分21秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;LL mod;class MARTIX{ public: LL mat[5][5]; MARTIX(); MARTIX operator * (const MARTIX &b)const; MARTIX& operator = (const MARTIX &b);};MARTIX::MARTIX(){ memset (mat, 0, sizeof(mat));}MARTIX MARTIX :: operator * (const MARTIX &b)const{ MARTIX ret; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j]; ret.mat[i][j] %= mod; } } } return ret;}MARTIX& MARTIX :: operator = (const MARTIX &b){ for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { this -> mat[i][j] = b.mat[i][j]; } } return *this;}MARTIX fastpow(MARTIX ret, LL n){ MARTIX ans; for (int i = 0; i < 2; ++i) { ans.mat[i][i] = 1; } while (n) { if (n & 1) { ans = ans * ret; } ret = ret * ret; n >>= 1; } return ans;}void Debug(MARTIX A){ for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { printf("%lld ", A.mat[i][j]); } printf("\n"); }}int main (){ LL a, b, n; while (~scanf("%lld%lld%lld%lld", &a, &b, &n, &mod)) { MARTIX A; A.mat[0][0] = a; A.mat[0][1] = 1; A.mat[1][0] = b; A.mat[1][1] = a; A = fastpow(A, n - 1); MARTIX F; F.mat[0][0] = a; F.mat[0][1] = 1; F = F * A; printf("%lld\n", (2 * F.mat[0][0]) % mod); } return 0;}
- hdu4565---So Easy!(矩阵)
- HDU4565 So Easy! (矩阵)
- hdu4565 so easy 矩阵
- HDU4565 So Easy!【矩阵连乘】【推导】
- hdu4565 So Easy!(矩阵快速幂)
- HDU4565 So Easy!【矩阵快速幂】
- HDU4565 So Easy!
- HDU4565 So Easy!
- 20130707 hdu4565 So Easy! 数学头脑+矩阵快速幂
- HDU4565 So Easy! 矩阵快速幂外加数学
- HDU4565-So Easy!(共轭运用+矩阵快速幂)
- HDU4565 So Easy!(数学+矩阵快速幂)
- HDU4565-So easy-数学推导化简递推矩阵快速幂
- HDU4565---So Easy!(矩阵快速幂(精度控制))
- hdu4565 So Easy! 广义斐波那契+矩阵快速幂+共轭构造
- So Easy! ——————hdu4565(矩阵快速幂)
- HDU4565/2013年长沙赛区So easy
- 2013长沙赛区第一题 HDU4565—So Easy!
- iOS开发数据库篇—SQLite的应用
- usb_modeswitch 自动转化的问题
- PAT 1032. Sharing (25)
- HDU 5185 Equation (DP)
- 第26天 3.12补【健身】
- hdu4565---So Easy!(矩阵)
- PAT 1039. Course List for Student (25)
- Socket编程实践(1) --TCP/IP简述
- 编写一个函数实现n^k,使用递归实现
- 九、Linux系统编程-进程(二)fork系统调用、复制进程映像、写时复制、孤儿进程和僵尸进程
- hdu 1394 Minimum Inversion Number(单点更新,区间求和)
- 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和
- PAT 1040. Longest Symmetric String (25)
- Windows系统Tomcat设置内存方法