hdu1588(斐波那契,矩阵连乘)
来源:互联网 发布:淘宝国外用户开户 编辑:程序博客网 时间:2024/05/04 04:05
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
让我们求
sn= f(b)+ f(k+b)+ f(2k+b)+……+ f(nk+b).
运用以上方法,矩阵连乘,解决问题!
(我的代码求别改,这段代码好不容易才改对的,本来的代码老是ce!!!改成这样我也不造哪里错了又改对了,求大牛指导)
#include<iostream>#include<stdio.h>using namespace std;const int max0=2;const int max1=4;typedef long long ll;int mod;typedef struct{long long zj[max0][max0];} matrix;typedef struct{long long zj[max1][max1];} matrix1;matrix I={1,0,0,1};matrix j2={0,1,1,1};matrix1 jj1={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};matrix1 jj2={0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1};matrix lc1(matrix a,matrix b){ matrix c; for(int i=0;i<max0;i++) { for(int j=0;j<max0;j++) { c.zj[i][j]=0; for(int k=0;k<max0;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c;}matrix a,b;matrix1 a0,b0;matrix quick1(ll n){ a=j2,b=I; while(n>=1) { if(n&1) b=lc1(b,a); n>>=1; a=lc1(a,a); } return b;}matrix1 lc2(matrix1 a,matrix1 b){ matrix1 c; for(int i=0;i<max1;i++) { for(int j=0;j<max1;j++) { c.zj[i][j]=0; for(int k=0;k<max1;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c;}matrix1 quick2(ll n){ a0=jj2,b0=jj1; while(n>=1) { if(n&1) b0=lc2(b0,a0); n>>=1; a0=lc2(a0,a0); } return b0;}int main(){ ll k,bbb,n; while(cin>>k>>bbb>>n>>mod) { matrix tm1=quick1(k); matrix tm2=quick1(bbb); jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1]; matrix1 tm=quick2(n); long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod; tmp=(tmp+mod)%mod; cout<<tmp<<endl; } return 0;}
原来ce代码,求指错!!:
#include<iostream>#include<stdio.h>using namespace std;const int max0=2;const int max1=4;typedef long long ll;int mod;typedef struct{long long zj[max0][max0];} matrix;typedef struct{long long zj[max1][max1];} matrix1;matrix j0={1,0,0,1};matrix j2={0,1,1,1};matrix1 jj1={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};matrix1 jj2={0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1};matrix lc1(matrix a,matrix b){ matrix c; for(int i=0;i<max0;i++) { for(int j=0;j<max0;j++) { c.zj[i][j]=0; for(int k=0;k<max0;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c;}matrix quick1(ll n){ matrix a=j2,b=j0; while(n>=1) { if(n&1) b=lc1(b,a); n>>=1; a=lc1(a,a); } return b;}matrix1 lc2(matrix1 a,matrix1 b){ matrix1 c; for(int i=0;i<max1;i++) { for(int j=0;j<max1;j++) { c.zj[i][j]=0; for(int k=0;k<max1;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c;}matrix1 quick2(ll n){ matrix1 a=jj2,b=jj1; while(n>=1) { if(n&1) b=lc2(b,a); n>>=1; a=lc2(a,a); } return b;}int main(){ ll k,bbb,n; while(cin>>k>>bbb>>n>>mod) { matrix tm1=quick1(k); matrix tm2=quick1(bbb); jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1]; matrix1 tm=quick2(n); long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod; tmp=(tmp+mod)%mod; cout<<tmp<<endl; } return 0;}
0 0
- hdu1588(斐波那契,矩阵连乘)
- HDU1588(矩阵连乘求和)
- hdu3117(斐波那契数列,矩阵连乘)
- HDU 3936 斐波那契性质矩阵连乘
- hdu1588 Fibonacci and 矩阵连乘
- HDU 3117(数论,斐波那契+矩阵连乘)
- HDU 1588 斐波那契数列数列变形和矩阵连乘
- 寂寞的素数 斐波那契数列性质+矩阵连乘+欧拉定理 经典啊
- 矩阵链乘+斐波那契+快速幂 专题
- POJ3070 Fibonacci (矩阵连乘)
- 矩阵连乘 动态规划(转载)
- 矩阵连乘(动规递推解决)
- 矩阵连乘问题(C++)
- 矩阵连乘问题(动态规划)
- Not Fibonacci(矩阵连乘)
- POJ 1651(矩阵连乘)
- 斐波拉契数列矩阵连乘(模板)
- 矩阵连乘问题(dp)
- hdu 4715 Difference Between Primes
- 常用Keytool 命令
- HDU——1984 Mispelling4
- Big Number(http://acm.hdu.edu.cn/showproblem.php?pid=1018)
- [转载]IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目
- hdu1588(斐波那契,矩阵连乘)
- codeforces 172B B. Pseudorandom Sequence Period(暴力)
- c++ 面试题
- Android状态栏变色/沉浸式状态栏 4.4及其以上兼容实现
- 第三章 IP:网际协议
- 使用python-sftp协议下载远程主机数据
- Codevs 3115 高精度练习之减法
- SGU104 Little shop of flowers(DP)
- 文件上传组件 FileUpload 的使用