Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Link to The Problem: https://codeforces.com/problemset/problem/166/E[^]

Problem Statement:

*You are given a tetrahedron. Let's mark its vertices with letters A, B, C, and D correspondingly.

An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn't stay idle. At each moment of time, he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can't stand on one place. You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (10e9 + 7).*

Input: The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.

Output: Print the only integer — the required number of ways modulo 1000000007 (10e9 + 7).

Example: Input n=2 , Output: 3 Input n=4, Output: 21

What I have tried:

My Approach to Problem: I have written a recursive code that takes two input n and present index, then I am traveling and exploring all possible combinations.


C++
#include<iostream>
using namespace std;
#define ll long long 
#define mod 10000000  
ll count_moves=0;
    ll count(ll n, int present)
    {   
        if(n==0 and present==0) count_moves+=1, count_moves%=mod;  //base_condition
        else if(n>1){                   //Generating All possible Combinations 
            count(n-1,(present+1)%4);
            count(n-1,(present+2)%4);
            count(n-1,(present+3)%4);
        }
        else if(n==1 and present) count(n-1,0);
    }
    int main()
    {
        ll n;  cin>>n;
        if(n==1) {  cout<<"0";  return;   } 
        count(n,0);
        cout<<count_moves%mod;
    }



But the problem is that I am getting Time Limit Error since Time Complexity of my Code is very high. Please Can anyone suggest me how can I optimize/Memoize my code to reduce its complexity?
Posted
Updated 14-Jul-20 10:26am
v5

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! The idea of these challenges is to test your knowledge; your abilities. To give you a chance to change the way you think about problems, and work out different solutions - and it wouldn't be at all fair for us to do it all for you as that would defeat the purpose!

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by thinking about your existing solution and why it's slow. Then start thinking about the problem and what other ways there might be to do the whole thing. There may be short cuts which reduce the need to "brute force" it as you are doing.
 
Share this answer
 
Comments
Pranshu Kashyap 14-Jul-20 15:42pm    
I didn't knew that this site is to tell you that if you are stuck at any problem then tell your doubts to your mentor and they will simply say solve yourself we are here just for posting nonsense answers.
If I would be able to answer my query myself why would I have asked you? It's not like I haven't approached the Question and asking you full solution neither I am demanding you step by step solution. Just I have asked please Give me hints what type of optimization I could do to reduce Time Complexity of my Code. I know my code is slow because of Exponential Complexity I expected a way to memoize my code. I am beginner and I need help at this stage to think in right direction but hola wow!! this site is full of Stupid people who couldn't understand this.

Although thanks for giving your Precious Time. I'll solve my doubt myself.
OriginalGriff 14-Jul-20 17:00pm    
Did you read what I said, or just get annoyed because I didn't give you the code? Your whole approach is why it's slow ...
Quote:
I didn't knew that this site is to tell you that if you are stuck at any problem then tell your doubts to your mentor and they will simply say solve yourself we are here just for posting nonsense answers.

In this site, we help you to fix your code, but your code show no try to use memoization, you describe no specific problem.
Quote:
Please Can anyone suggest me how can I optimize/Memoize my code to reduce its complexity?

What you don't understand in the principle of memoization.
Memoization is about remembering previous results and you are continuously recalculating thr same things.
Memoization - Wikipedia[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900