Recently I came across this problem in the IARCS server,here is an little excerpt from it,A well-known bandit who used to haunt the forests around Siruseri was  eliminated by the policemen a few months ago. There are several  stories about a valuable treasure that he is alleged to have buried  somewhere in the forest.The deceased bandit had several hideouts and the police found a lot of  things in these hideouts --- a number of guns, ammunition, knives, ...  Interestingly, in every hideout there was a strange looking machine.  This machine looked a bit like the weighing machine that is found in  Indian railway stations where you insert a coin and get a card with  your weight printed on it. The police had no idea what these machines  were meant for and since they were heavy and in the middle of the  forest they just left them there.Only one man knew that the clue to the buried treasure was in these  innocuous looking machines and that was Muttal. Muttal used to be part  of the dacoit's band of robbers but was thrown out for being too dumb.Here is how the treasure was to be found. One had to insert a 1 rupee  coin into one of these machines. This machine would then put out a  token and a card on which was printed the name of the machine to be  visited next. The token was then to be inserted into the machine whose  name was printed on the card. The new machine would, in turn, produce  another token and another card with a new destination printed on it,  and so on.If you started with by putting a 1 rupee coin in the correct machine  place and followed the sequence of machines indicated by the cards,  inserting each token produced by one machine into the next one,  eventually one of these machines would put out a map to the treasure.  Unfortunately, though, Muttal did not know which machine one should  begin with.Unknown to Muttal, the bandit had played one last joke on the world.  Knowing his end was near, he had reprogrammed these machines. There  was no longer any map. All that you got for inserting a token in any  machine was another token and a card leading you to the next machine.  So poor Muttal is going to spend the rest of his life inserting tokens  into machines and walking from one machine to another.You are given M machines that generate and respond to T different  kinds of tokens. We regard the 1 rupee coin also as one of the T types  of tokens. For each machine m and each token t you are told what  happens when t is inserted into m: that is, which token is produced by  m and what the next destination printed on the card is.Your task is the following. Given the identity of the machine where  Muttal starts his search and an integer N, identify the Nth machine  that Muttal will visit. Muttal always begins his search by inserting a  1 rupee coin into the first machine.For example suppose there are three machines and two kinds of tokens  and the description of the machines is as follows:We take T1 to represent the one rupee coin. If Muttal starts at  machine M1 then the sequence of machines he visits is M1, M2, M3, M1,  M2, M3, ... Thus, the fourth machine he visits is M1, the fifth  machine he visits is M2 and so on.So I have worked a little on the code , but I get Time limit exceeded for some last output , I was thinking of an modulo thing , but not sure of it, here is what I am working on,#include <iostream>#include <vector>int main(){    int machines , tokens , start , end;    std::cin >> machines >> tokens >> start >> end;    start--;end--;    std::vector<std::vector<int> >tokenTable(machines,std::vector<int>(tokens));    std::vector<std::vector<int> >machTable(machines,std::vector<int>(tokens));    //std::vector<std::vector<int> >visited(machines,std::vector<int>(tokens));    std::vector<int>visitedMach;    for(int i=0;i<machines;i++){        for(int j=0;j<tokens;j++){            int tok , mach;            std::cin >> tok >> mach;            tok--;mach--;            tokenTable[i][j] = tok;            machTable[i][j] = mach;        }    }    int moves =0 ,token = 0;    while(moves < end){        /*if(visited[start][token]){            int remaining = moves - visited[start][token]+1;            int modFac = (end-visited[start][token])%remaining;            if(modFac == 0){                modFac = remaining;            }            modFac = modFac + visited[start][token];            //std::cout << visited[start][token] << ' ' << moves <<' ' <<modFac<<std::endl;            std::cout <<visitedMach[modFac]+1<< std::endl;            return 0;        }        visited[start][token] = moves;        visitedMach.push_back(start);*/        int newTok = tokenTable[start][token];        int newStart = machTable[start][token];        token = newTok;        start = newStart;        moves++;    }    std::cout << start+1 << std::endl;    return 0;}The code in the comments is what I was thinking of doing but not sure of it, do you guys have some better approach for this problem?
Finding the nth machine encountered on the journey
c++;algorithm;programming challenge;array;time limit exceeded