HDU1588(快速幂)
来源:互联网 发布:快手特效软件 编辑:程序博客网 时间:2024/05/16 15:33
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1654 Accepted Submission(s): 720
Problem Description
Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.
Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.
Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
Input
The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
Each of them will not exceed 1,000,000,000.
Output
For each line input, out the value described above.
Sample Input
2 1 4 1002 0 4 100
Sample Output
2112//无法理解求和的Sum函数,哎~#include<iostream>#include<stdio.h>#define ll long longusing namespace std;struct Node{ll mat[2][2];};const int n=2;int k,b,nn,m;Node unit,qiqi;Node mul(const Node &a,const Node &b){Node c;int i,j,k;for(i=0;i<n;i++)for(j=0;j<n;j++){c.mat[i][j]=0;for(k=0;k<n;k++)c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%m;c.mat[i][j]%=m;}return c;}Node power(const Node &a,int x){int i,j;int t=x,d=0;while(t)d++,t>>=1;Node res=unit;for(i=d;i>0;i--){res=mul(res,res);if(x&(1<<(i-1)))res=mul(res,a);}return res;}Node add(const Node &a,const Node &b){Node c;int i,j;for(i=0;i<n;i++)for(j=0;j<n;j++)c.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%m;return c;}Node sum(const Node &a,int x)//A+A^2+……A^x{if(x==1)return a;Node temp=sum(a,x>>1);if(x&1){Node tmp=power(a,(x>>1)+1);return add(add(mul(temp,tmp),temp),tmp);}else{Node tmp=power(a,(x>>1));return add(mul(tmp,temp),temp);}}int main(){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) unit.mat[i][j]=(i==j); qiqi.mat[0][0]=1;qiqi.mat[0][1]=1;qiqi.mat[1][0]=1;qiqi.mat[1][1]=0;while(scanf("%d%d%d%d",&k,&b,&nn,&m)!=EOF){ Node res; res=power(qiqi,k);//A^k res=sum(res,nn-1);//A+A^2……+A^nn res=add(res,unit); Node ans=power(qiqi,b); res=mul(ans,res); printf("%I64d\n",res.mat[1][0]);}return 0;}
- HDU1588(快速幂)
- hdu1588 Gauss Fibonacci (矩阵快速幂)
- hdu1588 Gauss Fibonacci 矩阵快速幂
- HDU1588-Gauss Fibonacci(矩阵快速幂+等比数列二分求和)
- [HDU1588]Gauss Fibonacci(递推+矩阵快速幂)
- HDU1588 Gauss Fibonacci (矩阵快速幂+等比数列二分求和)
- hdu1588
- HDU1588
- hdu1588
- hdu1588 Gauss Fibonacci(矩阵快速幂+二分求矩阵等比和)
- Gauss Fibonacci hdu1588
- hdu1588 Gauss Fibonacci
- HDU1588 Gauss Fibonacci
- HDU1588解题报告
- HDU1588 Gauss Fibonacci 矩阵应用
- hdu1588之经典矩阵乘法
- HDU1588(矩阵连乘求和)
- hdu1588 Fibonacci and 矩阵连乘
- Excel2007 和excel2010实现分类汇总
- Android 语音输入API使用
- 杭电ACM 1000
- SEO学习(二)——搜索引擎简介及工作原理
- Vc控件用法总结之List Control
- HDU1588(快速幂)
- oracle单表查询简单语句
- 【女人睡醒后不能做的三件事】
- SEO学习(三)——搜索引擎的链接原理(排名过滤和调整)
- [5月21日的脚本] 在Windows 8开始按钮中创建一个切换用户磁贴 (VBScript)
- MongoDB 升级操作
- JAVA随笔--进程与线程
- iOS的三维透视投影
- 微软签约云南 将在云南建IT学院和软件中心