• Loading...

Problem 2: Fibonacci GCD

Points:

Small Case: 8 Answer: 144

Large Case: 16 Answer: 210345902

A Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 1, the first 10 terms will be:

1,1, 2, 3, 5, 8, 13, 21, 34, 55, ...

The recursive sequence is given by :

F(n)=F(n-1)+F(n-2)

The GCD of 2 numbers is the largest number which divides those two numbers completely ( i.e leaves no remainder).

The problem is to find the GCD(F(n),F(m)) mod 1000000007 ?

Small Case: n = 156 , m = 192

Large Case: n = 1234567890 , m = 9876543210


Solution:

The relation used to solve this problem is:
gcd(F(m),F(n)) = F(gcd(m,n))

We can find gcd(m,n) by euclidian algorithm , and then we have to find the value of F(gcd(m,n))%1000000007 .
For solving this we use the matrix representation of (x+2)th fibonacci number, which is :

which can be done in O(log(n)) time complexity.

Reference: Arthur T. Benjamin and Jennifer J. Quinn, Revisiting Fibonacci and Related Sequences, 2004


Code:


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<map>
#define ll long long int
#define MOD 1000000007ll
using namespace std;
ll n;
ll multiply(ll ans[][11],ll c[][11],ll n)
{
    ll ans1[11][11]={0};
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
        {
            ll x=0;
            for(ll k=0;k<n;k++)
            {
                x=(x+(ans[i][k]*c[k][j])%MOD)%MOD;
            }
            ans1[i][j]=x;
        }
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
            ans[i][j]=ans1[i][j];
    return 0;
}
ll power(ll y,ll n,ll c[][11],ll ans[][11])
{
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
            if(i==j)
                ans[i][j]=1;
            else
                ans[i][j]=0;
    while(y)
    {
        if(y&1)
        {
            multiply(ans,c,n);
        }
        multiply(c,c,n);
        y>>=1;
    }
    return 0;
}
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main()
{
    ll j,i,x1,y1,n,x,b[11][11]={0},c[11][11]={0},ans[11][11]={0};
        n=2;
        for(i=n-1;i>=0;i--)
        {
            c[i][0]=1;
        }
        for(i=0;i<n;i++)
        {
            b[0][i]=1;
        }
        for(i=1;i<n;i++)
            for(j=0;j<n;j++)
                if((i-j)==1)
                    b[i][j]=1;
                else
                    b[i][j]=0;
        scanf("%lld %lld",&x1,&y1);
        x=gcd(x1,y1);
        if(x<=n)
        {
            printf("%lld\n",c[n-x][0]);
        }
        else
        {
            power(x-n,n,b,ans);
            ll res=0;
            for(i=0;i<n;i++)
                res=(res+(ans[0][i]*c[i][0])%MOD)%MOD;
            printf("%lld\n",res);
        }
    return 0;
}

Submit your solution

You need to be logged in to submit a solution.

discussions


d2e7p1large case
Q:how to submit 0?
A:Answer is always a positive number for all problems in all cases
chiranjeevMeaning of mod
Q:What is the meaning of (F(n),F(m)) mod 1000000007 ?
A:You have to find the remainder when gcd(F(n),F(m)) is divided by 1000000007.
sinanLarge case problem
Q:I get a message saying that "you have successfully submitted your answer for the small case" then it goes back and asks for the small case again??
A:You have submitted the wrong answer for the small case.
gmunkhbaatarmnsmall case resubmit?
Q:I submitted and corrected small case. But i resubmitted (i mistaken think thats large case) small case on other output instead of large case. Should i resubmit small case?
A:Once you have corrected small case, you don't need to submit again. You can resubmit large case to update your answer. For large case, last answer submitted is evaluated.
porshValue of F(2)
Q:Is F(2) = 1 or 2? Is the given series 0 or 1 indexed?
A:F(2) = 1
sankalp91Large case
Q:How to submit solution for large test case?
A:You will be able to submit answers for large case once you have solved small case correctly

post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)