Poj-3070-fibonacci

Source: Internet
Author: User

Topic links

http://poj.org/problem?id=3070

Just learned the matrix, I wrote a Fibonacci, the problem looked at the input and output, directly affixed to the code, on AC. Matrices are a great way to do this.

Code

#include <stdio.h>
#include <string.h>
#define MOD 10000

__int64 A[5];
__int64 B[3][3];

__int64 Quick_mod (__int64 N)
{
__int64 i,j,k;
A[1]=1; A[2]=1;
b[1][1]=0; B[1][2]=1;
B[2][1]=1; B[2][2]=1;
while (n)
{
if (n&1)
{
__int64 S[3];
Memset (s,0,sizeof (s));
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
S[i]= (S[i]+a[j]*b[j][i])%mod;
}
}
for (i=1;i<=2;i++)
A[i]=s[i];
n--;
}
N=N/2;
__int64 Kk[5][5];
memset (kk,0,sizeof (KK));
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
for (k=1;k<=2;k++)
{
Kk[i][j]= (Kk[i][j]+b[i][k]*b[k][j])%mod;
}
}
}
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
B[I][J]=KK[I][J];
}
}
}
return a[1];
}

int main (void)
{
__int64 N;
while (scanf ("%i64d", &n) ==1)
{
if (n==-1)
return 0;
if (n==0)
{
printf ("0\n");
Continue
}
printf ("%i64d\n", Quick_mod (n-1));
}
return 0;
}

The method of using the problem can also

Code

#include <stdio.h>
#include <string.h>
#define MOD 10000

__int64 A[3][3],b[3][3];

__int64 Quick_mod (__int64 N)
{
__int64 i,j,k;
__int64 kk[3][3];
A[1][1]=1; a[1][2]=1;
A[2][1]=1; a [2] [2]=0;
B[1][1]=1; b[1][2]=1;
B[2][1]=1; b[2][2]=0;
while (n)
{
if (n&1)
{
memset (kk,0,sizeof (KK)),
for (i=1;i<=2;i++)
{
for (j=1;j <=2;j++)
{
for (k=1;k<=2;k++)
{
kk[i][j]= (kk[i][j]+a[i][k]*b[k][j])%mod;
}
}
}
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
A[i][j]=kk[i][j];
}
}
n--;
}
N=n/2;
Memset (kk,0,sizeof (KK));
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
for (k=1;k<=2;k++)
{
kk[i][j]= (kk[i][j]+ B[I][K]*B[K][J])%mod;
}
}
}
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
B[i][j]=kk[i][j];
}
}
}
return a[1][2];
}

int main (void)
{
__int64 I,j,k,k1,k2,n;
while (scanf ("%i64d", &n) ==1)
{
if (N==-1) return 0;
if (n==0)
{
printf ("0\n");
Continue
}
printf ("%i64d\n", Quick_mod (n-1));
}
return 0;
}

Poj-3070-fibonacci

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.