大斐波那契数取余

大斐波那契数取余

POJ 3070

POJ 3070 +  51Nod 1242 大斐波那契数取余-LMLPHP

#include "iostream"
#include "cstdio"
using namespace std; class matrix
{
public:
int a[][];
matrix()
{
a[][]=a[][]=a[][]=;
a[][]=;
}
}; matrix multi(matrix a,matrix b)
{
matrix temp;
int i,j,k;
for(i=;i<;i++)
for(j=;j<;j++)
{
temp.a[i][j]=;
for(k=;k<;k++)
temp.a[i][j]+=(a.a[i][k]*b.a[k][j]);
temp.a[i][j]%=;
}
return temp;
} matrix power(int n)
{
matrix temp,s;
temp.a[][]=temp.a[][]=;
temp.a[][]=temp.a[][]=;
while(n!=)
{
if(n%!=)
temp=multi(temp,s);
s=multi(s,s);
n=n/;
}
return temp;
} int main()
{
int n;
while(~scanf("%d",&n)&&(n!=-))
{ matrix t=power(n);
cout<<t.a[][]<<endl;
}
return ;
}

51Nod 1242 大斐波那契数取余

#include "iostream"
#include "cstdio"
using namespace std;
#define MOD 1000000009
#define LL long long
class matrix
{
public:
LL a[][];
matrix()
{
a[][]=a[][]=a[][]=;
a[][]=;
}
}; matrix multi(matrix a,matrix b)
{
matrix temp;
LL i,j,k;
for(i=;i<;i++)
for(j=;j<;j++)
{
temp.a[i][j]=;
for(k=;k<;k++)
temp.a[i][j]+=(a.a[i][k]*b.a[k][j]);
temp.a[i][j]%=MOD;
}
return temp;
} matrix power(LL n)
{
matrix temp,s;
temp.a[][]=temp.a[][]=;
temp.a[][]=temp.a[][]=;
while(n!=)
{
if(n%!=)
temp=multi(temp,s);
s=multi(s,s);
n=n/;
}
return temp;
} int main()
{
LL n;
while(~scanf("%lld",&n)&&(n!=-))
{ matrix t=power(n);
cout<<t.a[][]<<endl;
}
return ;
}
05-11 17:46