Cows of FooLand | Practice | GeeksforGeeks

Cows in the FooLand city are interesting animals. One of their specialities is related to producing offsprings. A cow in FooLand produces its first calve (female calf) at the age of two years and proceeds to produce other calves (one female calf a year).
Now the farmer Harold wants to know how many animals would he have at the end of N years, if we assume that none of the calves dies, given that initially, he has only one female calf?
Since the answer can be huge, print it modulo 109+7.
 
Example 1:
Input: N = 2
Output: 2
Explanation: At the end of 1 year, he will have 
only 1 cow, at the end of 2 years he will have 
2 animals (one parent cow C1 and other baby 
calf B1 which is the offspring of cow C1).

Example 2:
Input: N = 4
Output: 5
Explanation: At the end of 1 year, he will have
only 1 cow, at the end of 2 years he will have
2 animals (one parent cow C1 and other baby
calf B1 which is the offspring of cow C1). At
the end of 3 years, he will have 3 animals (one
parent cow C1 and 2 female calves B1 and B2, C1
is the parent of B1 and B2).At the end of 4 
years,he will have 5 animals (one parent cow C1,
3 offsprings of C1 i.e. B1, B2, B3 and one 
offspring of B1).

 
Your Task:
You don't need to read or print anything. Your task is to complete the function TotalAnimal() which takes N as input parameter and returns the total number of animals at the end of N years modulo 109 + 7.
 
Expected Time Complexity: O(log2N)
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 1018
+3
gauravtiwari30017 months ago
https://medium.com/codebrace/starred-problem-2-nth-fibonacci-number-in-log-n-time-821ea9a18296
This is a very good post on Medium explaining how to find a fibonacci Number in Logn time.
Though I am posting the solution. Don't just blindly copy, paste and submit.
Do give this post a read and understand it.
  
void multiply(long long int  mat[2][2],long long int m[2][2]){
   long long int temp[2][2];
   long long int x = 1000000007;
   temp[0][0]=mat[0][0]*m[0][0]+mat[0][1]*m[1][0];
   temp[0][1]= mat[0][0]*m[0][1]+mat[0][1]*m[1][1];
   temp[1][0]= mat[1][0]*m[0][0]+mat[1][1]*m[1][0];
   temp[1][1]= mat[1][0]*m[0][1]+mat[1][1]*m[1][1];
   mat[0][0]=temp[0][0]%x;
   mat[0][1]=temp[0][1]%x;
   mat[1][0]=temp[1][0]%x;
   mat[1][1]=temp[1][1]%x;
}

void mat_power(long long int mat[2][2],long long int n){
   if(n==1) return; // n==0 will never come
  
    mat_power(mat,n/2);
    multiply(mat,mat);
  
    long long int  m[2][2]={
       {1,1},
       {1,0}
       };   
    if(n%2!=0)
	    multiply(mat,m);
}


	
	
	////////////////////////////////////////////*
	int TotalAnimal(long long int n){
	    
	    long long int mat[2][2]={
       {1,1},
       {1,0}
       };   
    if(n==0) return 1;
    mat_power(mat,n+1);
    return mat[0][1];
	    // Code here
	}
};
+1
gauravtiwari30017 months ago
https://medium.com/codebrace/starred-problem-2-nth-fibonacci-number-in-log-n-time-821ea9a18296
This is a very good post on Medium explaining how to find a fibonacci Number in Logn time.
Though I am posting the solution. Don't just blindly copy, paste and submit.
Do give this post a read and understand it.
  
void multiply(long long int  mat[2][2],long long int m[2][2]){
   long long int temp[2][2];
   long long int x = 1000000007;
   temp[0][0]=mat[0][0]*m[0][0]+mat[0][1]*m[1][0];
   temp[0][1]= mat[0][0]*m[0][1]+mat[0][1]*m[1][1];
   temp[1][0]= mat[1][0]*m[0][0]+mat[1][1]*m[1][0];
   temp[1][1]= mat[1][0]*m[0][1]+mat[1][1]*m[1][1];
   mat[0][0]=temp[0][0]%x;
   mat[0][1]=temp[0][1]%x;
   mat[1][0]=temp[1][0]%x;
   mat[1][1]=temp[1][1]%x;
}

void mat_power(long long int mat[2][2],long long int n){
   if(n==1) return; // n==0 will never come
  
    mat_power(mat,n/2);
    multiply(mat,mat);
  
    long long int  m[2][2]={
       {1,1},
       {1,0}
       };   
    if(n%2!=0)
	    multiply(mat,m);
}


	
	
	////////////////////////////////////////////*
	int TotalAnimal(long long int n){
	    
	    long long int mat[2][2]={
       {1,1},
       {1,0}
       };   
    if(n==0) return 1;
    mat_power(mat,n+1);
    return mat[0][1];
	    // Code here
	}
};
0
shikhars31457 months ago
Can anybody explain how this question is same as fibonacci series?
+2
aijazhera27627 months ago
Easy explanation for solving the Fibonacci Sequence with Matrix Exponentiation
https://www.youtube.com/watch?v=EEb6JP3NXBI
0
aijazhera2762
This comment was deleted.
0
aijazhera2762
This comment was deleted.
-1
mridultyagi80
This comment was deleted.
+3
ritintiwari4177 months ago
If you Program is working till 43 and giving you wrong answer for above, refer this article : https://www.geeksforgeeks.org/modulo-1097-1000000007/
+1
nishaanjum119
This comment was deleted.
0
bhupeshbhatt82227 months ago
I am using the concept of fibonacci number, f(n) = f(n) + f(n-1)
This code is working fine for smaller numbers , but not workinbg for larger numbers . Can anyone tell the isssue?
 
  int TotalAnimal(long long int N){    // Code here        if(N==1||N==2||N==3)        return N;            int prev = 1, p_prev=1, curr;        long long int i;    int sum =3;            for( i =3 ; i<N; i++)    {        curr= prev + p_prev;        p_prev= prev;        prev= curr;        sum+=curr;    }       return sum;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.