We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Lexicographic paths
Lexicographic paths
Sort by
recency
|
7 Discussions
|
Please Login in order to post a comment
def C(n, r): ans = f(n) / f(r) / f(n-r) return ans
def solve(x, y, k): ans = "" while k > 0: if k < C(x+y-1, y): ans += "H" x -= 1 else: ans += "V" k -= C(x+y-1, y) y -= 1 ans += x * "H" ans += y * "V" return ans
C++
Iterative solution. The first lexigraphic solution is always x 'H's followed by y 'V's. If we consider when the leftmost V moves left, for y = 2 it's 1,3,6,10,15..., for y=3 it's 1,4,10,20,35... In general it's the yth figurate number. Figurate numbers are choose(n+y - 1, y) so the reason for this should be apparent (the leftmost V moves after all combinations to its right are exhausted).
The V's to the right follow the same pattern, repeated every time the V to the left moves.
So, solution is find maximum yth figurate number less than k. The index of this number tells us where the leftmost V is. Subtract that from k. Then find maximum y-1st figurate number less than the remainder, and so on.
13, 14, 15, 16 timed out.
Any clue on this ??
include
int X, Y, order, odr, limit, bfound= 0;
int f[50];
void calAns(int idx, int x, int y);
void init();
void init()
{
}
int main()
{
}
void calAns(int idx, int x, int y)
{
}
reminds me of a question on project euler :P