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 100 2 0 4 100
Sample Output
21 12
让我们求
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;
}