hdu 4565 (快速矩阵幂)
来源:互联网 发布:软件蓝图设计 编辑:程序博客网 时间:2024/06/08 14:27
So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4047 Accepted Submission(s): 1338
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!
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 20132 3 2 20132 2 1 2013
Sample Output
4144
题目大意:
给出a,b,n,m,求出的值,
思路:取模前有更号,所以无法直接计算,我们发现
0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231
所以 0 <| a+sqrt( b ) | < 1
可得表达式:
构造矩阵:
s[n] 2a b-a*a s[2]
= *
s[n-1] 1 0 s[1]
#include <bits/stdc++.h>using namespace std;const int N=2;long long int mod;struct M{long long int m[N][N];};M I={ //单位矩阵 1,0,0,1};M fun(M a,M b) //a*b矩阵 {M c;for(int i=0;i<N;i++){for(int j=0;j<N;j++){c.m[i][j]=0;for(int k=0;k<N;k++){c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;}c.m[i][j]%=mod;}}return c;}M power(M A,long long int k) //快速矩阵幂 {M ans=I,p=A;while(k){if(k&1){ans=fun(ans,p);k--;}k>>=1;p=fun(p,p);}return ans;}int main(){long long int a,b,n,ret;M A,ans; //A矩阵为所求矩阵 while(~scanf("%lld %lld %lld %lld",&a,&b,&n,&mod)){A.m[0][0]=2*a%mod;A.m[0][1]=((b%mod-a*a%mod)%mod+mod)%mod;A.m[1][0]=1;A.m[1][1]=0;if(n==1) //特殊情况 {cout<<2*a%mod<<"\n";continue;}ans=power(A,n-2); //注意 是n-2 ret=(ans.m[0][0]%mod*2*(a*a%mod+b%mod)%mod+2*a%mod*ans.m[0][1]%mod)%mod;cout<<ret<<"\n";}return 0; }
0 0
- HDU 4565 矩阵快速幂
- hdu 4565(矩阵快速幂)
- HDU 4565 矩阵快速幂
- hdu 4565(矩阵快速幂)
- hdu 4565 (快速矩阵幂)
- HDU 4565 So Easy! (矩阵快速幂)
- hdu 4565 公式推导+矩阵快速幂
- 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 推倒公式+矩阵快速幂
- HDU 4565 (数学推导 矩阵快速幂)
- 矩阵快速幂——HDU 4565
- HDU 4565 so easy ! 矩阵快速幂
- 图的邻接表示法Java版
- JUST SO SO之 MySQL
- Tornado使用Ueditor的一些操作
- Python随笔1-练习题
- Jquery如何获取select选中项 自定义属性的值?
- hdu 4565 (快速矩阵幂)
- ARP协议
- nyoj37 回文字符串
- RocketMQ最佳实践(二)4.0版本/集群
- 2017-04-12 DBA日记,频繁commit导致的log file sync的诊断
- Python+Selenium练习篇之30-获取当前页面全部图片信息
- Java虚拟机-----运行时栈结构
- 算法导论 计数排序
- 垃圾回收基础