hdu 4565(矩阵快速幂)
2016-06-07 13:20
357 查看
So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3675 Accepted Submission(s): 1194
Problem Description
A sequence Sn is defined as:
![](http://acm.hdu.edu.cn/data/images/C468-1001-1.jpg)
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!
![](http://acm.hdu.edu.cn/data/images/C468-1001-2.jpg)
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长沙赛区全国邀请赛——题目重现
#include <bits/stdc++.h> using namespace std; #define ll long long ll Mod; struct Matrix { ll ma[2][2]; void init(ll a,ll b,ll c,ll d){ ma[0][0]=a;ma[0][1]=b; ma[1][0]=c;ma[1][1]=d; } Matrix(){ memset(ma,0,sizeof(ma)); } Matrix operator * (const Matrix &m){ Matrix res; for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) res.ma[i][j]=(res.ma[i][j]+m.ma[i][k]*ma[k][j])%Mod; return res; } }; Matrix quick_powm(Matrix m,ll n) { if(n==1)return m; Matrix res=quick_powm(m,n/2); Matrix ans=res*res; if(n%2==1) ans=ans*m; return ans; } int main() { ll a,b,n; while(~scanf("%lld%lld%lld%lld",&a,&b,&n,&Mod)) { Matrix m,res; m.init(a,1,b,a); res=quick_powm(m,n); printf("%lld\n",(2*res.ma[0][0])%Mod); } return 0; }
相关文章推荐
- JDK1.8的十大新特性总结
- SQL分离附加数据库
- dubbo框架简介2
- dorcker容器
- HDU 5504 GT and sequence
- 【OpenCV_11】利用OpenCV检测鼠标点击及移动 Detect Mouse Clicks and Moves
- linux基础(37)--xfs文件系统的备份和恢复--RHEL7.2
- Qt:用 __thread 关键字让每个线程有自己的全局变量
- Dubbo框架简介
- hdu 4291(矩阵快速幂+循环节)
- 【iOS开发】事件处理之手势识别(一)
- 数据类型
- 自定义datetimepicker日期控件
- 安卓webview实现应用内嵌入网页
- C++中实现split函数功能
- Move Zeroes
- Mybaits多数据源配置
- cmake同时生成动态库与静态库的方法
- c51软复位,实在经典,分析实在透彻
- hdu 5695(拓扑排序)