POJ——4565So Easy!(矩阵快速幂)
来源:互联网 发布:淘宝达人报名入口 编辑:程序博客网 时间:2024/05/20 22:40
So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3642 Accepted Submission(s): 1185
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
难点在于这题是考数学的,只能根据它a与b的范围推出
其中Cn=ceil(a+sqrt(b))
C0=2,C1=2*a,还有一个坑点就是最后输出的答案要向正方向取模,WA好几次
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<stack>#include<cmath>#include<queue>#include<set>#include<map>using namespace std;typedef long long LL;#define INF 0x3f3f3f3fLL mod;struct mat{ LL pos[2][2]; mat(){memset(pos,0,sizeof(pos));}};inline mat operator*(const mat &a,const mat &b){ mat c; for (int i=0; i<2; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) c.pos[i][j]+=((a.pos[i][k]%mod)*(b.pos[k][j]%mod)+mod)%mod; return c;}inline mat matpow(mat a,int b){ mat bas,r; r.pos[0][0]=r.pos[1][1]=1; bas=a; while (b!=0) { if(b&1) r=r*bas; bas=bas*bas; b>>=1; } return r;}int main(void){ LL a,b,n; while (~scanf("%lld%lld%lld%lld",&a,&b,&n,&mod)) { mat one,t; one.pos[0][0]=2*a;one.pos[1][0]=2; t.pos[0][0]=2*a;t.pos[0][1]=-(a*a-b); t.pos[1][0]=1;t.pos[1][1]=0; t=matpow(t,n); one=t*one; printf("%lld\n",(one.pos[1][0]%mod+mod)%mod); } return 0;}
- POJ——4565So Easy!(矩阵快速幂)
- HDU 4565——So Easy!(矩阵快速幂乘法)
- HDU 4565矩阵快速幂—— So Easy!
- HDU 4565 So Easy! (矩阵快速幂)
- hdu 4565 - So Easy!(矩阵快速幂)
- hdu 4565 So Easy! 矩阵快速幂
- HDU 4565 So Easy!(矩阵快速幂)
- HDU 4565 So Easy!(矩阵快速幂)
- HDU 4565So Easy! 矩阵快速幂
- hdu 4565 So Easy!(矩阵快速幂)
- HDU - 4565 So Easy! 矩阵快速幂
- HDU 4565 so easy ! 矩阵快速幂
- So Easy!(矩阵快速幂)
- hnu13150(hdu 4565)SO EASY!(矩阵快速幂)
- hdu 4565 So Easy!(矩阵+快速幂)
- hdu - 4565 - So Easy!(矩阵快速幂)
- HDU 4565 So Easy!(数学+矩阵快速幂)
- hdu 4565 So Easy!(推公式&矩阵快速幂)
- URLDecoder类API_java.net.URLDecoder
- 中文写入数据库乱码及Incorrect string value: '\xF0\x9F...' for column 'XXX' at row 1解决
- django model的get和filter方法的区别
- 学习篇---自定义ZoomImageView
- 谈情--读《白马啸西风》有感
- POJ——4565So Easy!(矩阵快速幂)
- ViewConfiguration.getScaledTouchSlop () 用法
- HTML入门笔记案例展示(1)
- 使用并查集判断无向图是否连通
- 使用VS2010自定义C++模板
- Idea的插件
- 初探RxJava
- Qt(三)--- 实现QQ登陆界面头像状态栏的功能
- 算法笔记——【动态规划】最大子段和问题