Given a short program of nested loops bounded by n, outer loop variables, or small constants, output the exact count of lag executions as a polynomial in n.
Medium7MathCombinatoricsSimulationImplementationNo attempts yetTime limit2sMemory limit256 MBFrederick writes every contest solution in Fygon, his favorite language. The Fygon interpreter is slow, so he gets Time Limit Exceeded even when his algorithm is asymptotically optimal, and he squeezes solutions into the limit with constant factor tricks. To judge which trick is worth the effort he needs the exact number of operations his program performs.
Fygon has only two statements. The first one is lag, which stands in for almost any other statement. The second one is a for loop.
for <variable> in range(<limit>):
<body>
The loop variable starts at 0 and runs through the integers smaller than <limit>. A variable is a lowercase letter from a to z, and <limit> is either a variable that is already defined or a positive integer constant. The <body> of a loop is indented by four spaces and holds at least one statement.
The program reads its input into the variable n. That variable has a special meaning and cannot be used as a loop variable.
Given a Fygon program, find how many lag operations it performs as a formula in n.
The input is the whole Fygon program. No two loops use the same loop variable. Every variable that appears inside a range is either n or a variable declared by an enclosing loop. The program has at most 20 statements, and at most 6 of them are loops. Every integer constant is between 1 and 9. Each nesting level is indented by four more spaces, and every line is either lag or for <variable> in range(<limit>):.
Let f(n) be the number of lag operations. f is a polynomial in n with rational coefficients. Take its expanded form f(n)=cdnd+cd−1nd−1+⋯+c0 and print it on one line, without spaces, by the rules below.
Print only the terms whose coefficient is not zero, in decreasing order of the exponent. The term with exponent k and coefficient c is written as ∣c∣ followed by k copies of *n. Write ∣c∣ as a reduced fraction p/q with a positive denominator, and write only p when the denominator is 1. Keep the coefficient even when it equals 1. The first term takes a leading - only when its coefficient is negative, and every later term takes + when its coefficient is positive and - when it is negative. If f is identically zero, print a single 0.
For example, print 11/2*n*n-1/2*n+5 for f(n)=211n2−21n+5, and print 0 for a program whose lag never runs.