## August 2010 Contest Problem Editorials |

**Problem Tester** for the contest was Ajay Somani.

**Obstacle Course (written by David Stolp).** The Editorials can be found here.

**Exit code (written by Anh Duong Quang).** The Editorials can be found here.

**Optimizing Production and Sales (written by David Stolp).** The Editorials can be found here.

**Genetics (written by Ivan Mistreanu).** The Editorials can be found here.

**Stepping Numbers (written by Michael Dorfling).** The Editorials can be found here.

**Swarm of Polygons (written by Ivan Mistreanu).** The Editorials can be found here.

## Comments

**Please login at the top to post a comment.**

## please explain the term

dabbcomputers@ 11 Aug 2010 07:07 PMplease explain the term lexicological in exit code clearly. and what is the output for the case 12 11 111 92.

thanx.

## Re: StepNums, Nice problem

subra@ 11 Aug 2010 07:51 PMRe: StepNums, Nice problem and solution.

I tried diagonalizing the adjacency matrix to take advantage of faster exponentiation of diagonal matrix. But the diagonal entries(roots of P(x)), sqrt(3) did not exist in Z_mod. Anybody sees a way of converting to an equivalent diagonal matrix or is this approach doomed ?

## @Subrahmanyam Velaga: What

MichaelD@ 11 Aug 2010 08:04 PM@Subrahmanyam Velaga: What happens if you try and work in Z_p[sqrt(3)]? i.e. work with numbers of the form a + b sqrt(3), with a and b in Z_p, like in the intended solution.## nice problem :)

imrankane2005@ 11 Aug 2010 09:12 PMnice problem :)

## 1st problem is not mine :p

anhdq_adm@ 11 Aug 2010 09:21 PM1st problem is not mine :p

## A few comments: 1. Optimizing

tomek@ 11 Aug 2010 10:22 PMA few comments:

1. Optimizing Production and Sales: All these people tied for first have optimal answers (or at least within the rounding error).

3. Obstacle Course: Prim's algorithm has complexity O(N^2).

4. Exit Code: O(10^(n/2) * log 10^(n/2)) = O(10^(n/2) * n).

## @Michael Dorfling: Makes

subra@ 11 Aug 2010 10:24 PM@Michael Dorfling: Makes sense. Now I see that the solution you mentioned is pretty similar to diagonalization working in Z_p[sqrt(3)]. The fact that n is even allows us to convert the uglier radicals (sqrt( 2 + sqrt(3) )) into manageable 2 + sqrt(3).

I searched for patterns initially and found that for small prime moduli, a small look back (I observed 4, not 5) determined the next number and the sequence also had a period. So, I suspected a form a1^n + a2^n + .. + a4^n. But, I abandoned it since each ai has a period of P-1 and the whole expression should have a period P-1 which was not the case. I did not realize that it could be (a1,b1)^n.

## I don't uderstand what are

pdwd@ 11 Aug 2010 10:51 PMI don't uderstand what are nCycle and c1,..,cb in task Swarm of Polygons. Suppose our cycle has length l, it consists of xi,..,x[i+l-1] points on each side and there are n cycles, can somebody please express formula for R2 using these symbols?

## To avoid confusion let's say

pdwd@ 11 Aug 2010 11:13 PMTo avoid confusion let's say there are c cycles. So (n-|tail|-|head|)/l = c

## Genetics: Naive

burdakovd@ 11 Aug 2010 11:16 PMGenetics:

Naive implementation of Cartesian trees should get TLE on worst test cases (because when we cross the same DNA many times - tree will contain many nodes with equal priorities ("y" keys), and may become imbalanced, height==5000 in worst case).

There are some randomisations in merge procedure, which reduce tree height. But unfortunately they weren't required to get AC. I wish there were challenge system on codechef.

Examples of worst case:

actually input: https://docs.google.com/leaf?id=0Bxx7QSPZZj2gYWRhZWM5NjUtZDM3Yi00ODE3LTlmYTctZmJhMzNmZTIwMjJm&hl=ru

test generator: http://codepad.org/4FooJo83

## @Subrahmanyam Velaga: Yes,

MichaelD@ 11 Aug 2010 11:21 PM@Subrahmanyam Velaga: Yes, it's much the same thing, but I prefer your approach. It shows directly that there is a formula that involves exponentiating the eigenvalues only. That the formula mod p can be calculated exactly using integer arithmetic still has to be checked, but of course it turns out that it can. Another advantage of your approach is that it can work even if the answer is not a linear combination of entries of a power of A.

It's interesting that you mention the period: I went to some trouble to find a p such that the sequence has period greater than p. Such p's are surprisingly rare. You can show that 2+sqrt(3) has order p-1 or p+1 in Z_p[sqrt(3)], but for most primes p (near 2^32 anyway) it is p-1. 4294967143 is the largest prime below 2^32 for which the order is p+1.

## why no editorials for July 10

theeporithirumugam@ 11 Aug 2010 11:40 PMwhy no editorials for July 10 Contest??????

## The July editorial was done

triplem@ 12 Aug 2010 02:29 AMThe July editorial was done ages ago.. http://www.codechef.com/wiki/editorials-codechef-contest-problems

## Regarding the periods, you

subra@ 12 Aug 2010 08:43 AMRegarding the periods, you answered my next question :)

During the contest, I initially mistook MOD for 2^32 - 1 and factored it and obtained 3, 5, 17, 257, 65537 as factors; (2^(2^t) + 1) , t = 0, 1, 2, 3, 4 (which is an identity, after i thought about it :) ).

And for t=0,1,2,3 the periods for s_n were 6, 12, 144, 33024 which are all (p-1)*(p+1)/2 (except for p=3), meaning that 2 + sqrt(3) has period p+1 in Z_p[sqrt(3)] for these primes. I noted O(p^2) growth and abandoned this approach as t=5 would have period of ~2^32.

So, it seems I inadvertently ran into the moduli with 2+sqrt(3) having a period p+1 and I did not bother working out the period for 2^16 + 1.

Since you mentioned that primes where 2 + sqrt(3) has period p+1 in Z_p[sqrt(3)] are rare, i suppose it does raise an interesting question: P = If 2^(2^t) + 1 is a prime ( are there only finite of these ? I don't know.. ) then does 2+sqrt(3) have an order P+1?

## @Przemyslaw Derengowski :

subra@ 12 Aug 2010 09:29 AM@Przemyslaw Derengowski : Here is a small example which might clarify things.

Consider a few partitions of b = 7:

Partition (k1, k2,..., ks) (c1, c2,..., cb)

1+2+2+2 (1,2,2,2) (1,3,0,0,0,0,0)

2+2+3 (2,2,3) (0,2,1,0,0,0,0)

1+3+3 (1,3,3) (1,0,2,0,0,0,0)

1+2+4 (1,2,4) (1,1,0,1,0,0,0)

1+6 (1,6) (1,0,0,0,0,0,1)

7 (7) (0,0,0,0,0,0,1)

## @Przemyslaw Derengowski :

subra@ 12 Aug 2010 09:31 AM@Przemyslaw Derengowski : nCycle = c in your notation.

## @Subrahmanyam: Very

MichaelD@ 12 Aug 2010 03:36 PM@Subrahmanyam: Very interesting. Numbers of the form 2^(2^t)+1 are Fermat numbers. The only known values of t for which they are prime are precisely t=0,1,2,3,4 and it is believed there are no more. That the order of 2+sqrt(3) is p+1 if p=2^(2^t)+1 is prime follows from Pepin's test.

## @Michael Dorfling: It does

subra@ 12 Aug 2010 06:37 PM@Michael Dorfling:It does indeed follow from Pepin's test. I had no idea about this test or the Fermat's numbers. Thank you for pointing those out :)

Summarizing (Let A be the adjacency matrix of a undirected graph)

To obtain a closed form for a function F(A^n) where F(B) is a linear function of its entries Bij, look at linear combinations of the nth powers of eigen-values.

To "compute" a function F(A^n), where F(B) is a (not necessarily linear function) of its entries Bij, diagonalize A: A = X * D * inv(X), with D diagonal and output F( X* D^n * inv(X) ). The difficulty arises in keeping the computations modulo P. With some luck, the entries of D and X (and thus inv(X)) might be members of a different (finite) field and computations could be done in that system and casted back to P.

Would you know if there are infinite analogues ? Infinite graphs with locally specified adjacency matrices and count replaced with probability/expectation. I am thinking random walks.

## @Subrahmanyam Velaga: Thx for

pdwd@ 12 Aug 2010 09:31 PM@Subrahmanyam Velaga: Thx for help :)## I think you can always do

MichaelD@ 12 Aug 2010 11:31 PMI think you can always do computations in some other system, namely R=Z_p[x1, . . , xn] where the xi's are the eigenvalues. It would be nice to have a proof though. As for infinite analogues, I have no idea.

## @Michael Dorfling - I found

triplem@ 13 Aug 2010 02:28 AM@Michael Dorfling - I found Stepping Numbers to be one of the nicest problems I've solved in a long time :)

## @Michael Dorfling: I am not

subra@ 13 Aug 2010 05:49 AM@Michael Dorfling: I am not sure if detecting a system is easy, even if such a system does exist. Given that Z_3[sqrt(3)] is not a field (no multiplicative inverse for (0,x), (2,1) has order 6 etc.), how would computation proceed if MOD were 3 ?

## @Stephen Merriman: Glad you

MichaelD@ 13 Aug 2010 04:12 PM@Stephen Merriman: Glad you liked it :)

@Subrahmanyam Velaga: I think I've got it. First let's forget about MOD, and just show that there is a formula for F(A^n) involving only rationals and eigenvalues, assuming A is rational and symmetric. The smallest subfield of C containing Q and all the eigenvalues of A is a finite extension of Q, so it equals Q(a) for some a. Every element of Q(a) has the form b_0+b_1*a+b_2*a^2+ . . + b_{n-1}a^{n-1}, where all b_i's are rational. In particular, the eigenvalues have that form.

Since A is real and symmetric it is diagonalisable. Consider the standard diagonalising procedure: You compute an eigenvector corresponding to each eigenvalue and let X be the matrix with columns consisting of these eigenvectors. Then X D inv(X)=A. The eigenvectors are obtained by solving the linear systems (A-lambda*I)u=0, which shows that all entries of X are in Q(a). Now each entry of X A^n inv(X) is a linear combination of n-th powers of the eigenvalues, with all coefficients in Q(a), so we have a formula for F(A^n) that can be computed in Q(a). (We actually need some assumptions on F.)

Each element of Q(a) can be written as (c_0+c_1*a+c_2*a^2+ . . + c_{n-1}a^{n-1})/q where q and all c_i's are in Z. It follows that F(A^n)=p^n/((q^n)*r)*(d_0*l_0^n+d_1*l_1^n+ . . + d_{n-1}*l_{n-1}^n) where p, q, r are in Z and all d_i's and l_i's are in Z[a]. The only problem with computing this modulo some M is if q or r is not relatively prime to M. Can you find a way to deal with that?

## @Michael Dorfling: I have

subra@ 13 Aug 2010 07:57 PM@Michael Dorfling: I have read through what you've written and I am still thinking about it. Its been a fair few years (5+) since I last studied a bit of field theory, so I shall claim ignorance straight up :)

There seems to be a bit of a mix up in using 'n' (the exponent) and 'm' the size of the matrix. So, the dimension of the number field Q(a) is probably 'm' and not 'n' the exponent. I tried to clear up at http://docs.latexlab.org/docs?#1MY7mvVHH4E5Hb7hfE3RtQvXQub8AOQ-NjpYvnFUNT3k . Its a latex doc I've made based on what you wrote above and you can edit it. Its more convenient to eyeball the equations.

The last paragraph, I am still trying to make sense.

## @Michael Dorflin: For now,

subra@ 13 Aug 2010 08:23 PM@Michael Dorflin: For now, we could restrict F(B) to be a polynomial in Bij. We could probably use more field extensions to handle different types of F, but polynomial Fs should let us work with Q(a).

## Sorry about using n for two

MichaelD@ 13 Aug 2010 08:57 PMSorry about using n for two different things.

I'm getting an invalid document id when trying to access that doc of yours. Being able to use latex will be awesome.

## Seems like latexlabs still

subra@ 13 Aug 2010 11:07 PMSeems like latexlabs still doesn't have real collaboration, I thought they did from their headlines.

http://www.scribtex.com/ is a pretty neat collaborative latex editor as well as provides online compilation into pdf, so if you have a simple browser pdf plugin, you are set to go.

If you are willing to sign up (it is free), let me know your username and I can add you to the list of collaborators for the document.

## Ok, signed up with username

MichaelD@ 14 Aug 2010 12:52 AMOk, signed up with username michaeld.

## I've shared a project with

subra@ 14 Aug 2010 01:49 AMI've shared a project with you. My id is vlsubra. I am guessing you will be notified.

## It follows that

subra@ 14 Aug 2010 02:38 AMIt follows that F(A^n)=p^n/((q^n)*r)*(d_0*l_0^n+d_1*l_1^n+ . . + d_{m-1}*l_{m-1}^n) where p, q, r are in Z and all d_i's and l_i's are in Z[a].Sorry if this is trivial, I still don't get it; there seem to be many undetermined quantities.

A few things as I understand:

1) q is fixed (as the lcm of all denominators of all coefficients of all the eigen values in Q(a) )

2) a is "unknown". We want to "eliminate" it and alleviate determining a candidate 'a'. Is it true that if A has characteristic polynomial C, then C(a) = 0? (i.e. is it correct to treat the extension as quotient ring Q[x]/C ?)

I don't get how the expression you gave is sufficient in itself.

if C = (x^2-2)*(x^2-3), can you outline the steps of the computation ?

## Ok, you were showing

subra@ 14 Aug 2010 01:29 PMOk, you were showing "existence" of a field in which computations can be done "exactly" i.e. with essentially integer arithmetic. (for MODs which satisfy some conditions, like rel prime to q)

I was thinking that you were suggesting a constructive algorithm. As of what I understand now, one would still need to identify some 'a' so as to be able to define the operations on the field elements. (like (a,b) *(c,d) = (ac+3bd, ad+bc) in the a + b*sqrt(3) case) I still can't see a way of automatically identifying appropriate 'a' (factorization isn't easy, is it?).

## Sorry. I was still thinking

MichaelD@ 14 Aug 2010 04:24 PMSorry. I was still thinking of linear combinations. F(A^n) would involve products of l_i's, and q would be a power of the lcm of denominators of eigenvalues. There is also still some confusion about m. Let d be the degree of Q(a). Then every element of Q(a) has the form b_0+b_1*a+ . . +b_{d-1}*a^{d-1}. It is not true that C(a)=0, but p(a)=0 where p is the minimal polynomial for a.

This approach isn't necessarily faster than computing A^n directly. The idea is that it can be considerably faster. When d is small, for instance, or if exponentiating eigenvalues simplifies in some other way.

I was only showing existence, yes. It's a general approach, not an algorithm. You may not have to explicitly find an a. All you really need is a polynomial p (of degree less than m) such that Q(a) contains all eigenvalues of A for some root a of p, which then defines multiplication via p(a)=0. I don't know if finding such a polynomial is any easier than finding a. It should be, since there need not even be a radical expression for a.

## d = number of non rational

subra@ 14 Aug 2010 05:38 PMd = number of non rational roots of C, correct? I was imagining extending the field one root at a time (actually two since they occur in conjugates), and you need to increase the dimension as many times as the non rational roots. So, d < m in any case.

Yes, seems like the time savings may not be significant. Assuming we can find a 'p' via which we define multiplication, a straightforward multiplication would be O(d^2). So, with this method it would be O(m * d^2 * ln(n) ) versus O( m^3 * ln(n) ) for the straightforward multiplication.

So, while we are reducing the number of multiplications, we are paying more per multiplication.

However, this is a legitimate and potentially good approach as you pointed out, when either 'd' is very small or if the multiplication (or exponentiation) turns out to be simple enough. (for example, MOD = 2 i.e. we want the parity)

## d would be at most the number

MichaelD@ 15 Aug 2010 03:55 PMd would be at most the number of non-rational roots of C, I think. It can be much less, like when C=(x^2-2)(x^2-3) . . (x^2-k).

I don't know if it helps any, but the eigenvalues are algebraic integers, and we can also take a to be an algebraic integer. The integers of Q(a) form a free abelian group of rank d. Maybe you can use that to speed up computation.

## Can some body throw some more

shadow@ 18 Aug 2010 01:14 AMCan some body throw some more light on ECODE.

Thanks in advance!!

## @Gunjan Sharma: this

medium@ 29 Aug 2010 04:19 PM@Gunjan Sharma: this technique is called meet in the middle. Instead of investigating all possible 10^n sequences we build all prefix sequences a1a2...an/2 and suffix an/2+1an/2+1...an . Now we should glue it somehow. Binary search fits our needs because if the prefix sequence has remainder r1 and we know that r1+r2 = G, then the suffix sequence has to have remainder r2 = G-r1. We can find whether it has this remainder via binary search in sorted array.

## Thanks Max Now thats makes

shadow@ 29 Aug 2010 08:45 PMThanks Max

Now thats makes more sense to me!!!! :)

## Thanks for post this helpful

poojakro45@ 18 Jul 2015 03:41 PM## This article is really

poojakro45@ 18 Jul 2015 03:41 PM## Thanks for all your

poojakro45@ 18 Jul 2015 03:41 PM## This article is really

sonumishraseo@ 22 Jul 2015 12:47 PM## Thanks for all your

sonumishraseo@ 22 Jul 2015 12:48 PM## Thanks for all your

sonumishraseo@ 22 Jul 2015 12:49 PM## This article is really

sonumishraseo@ 22 Jul 2015 12:50 PM## Thanks for Export Packers and

sonigupta@ 26 Aug 2015 11:47 AM## Thanks for Export Packers and

sonigupta@ 26 Aug 2015 11:49 AM## Thanks for mour information

sonigupta@ 28 Aug 2015 11:40 AM## Thank you very much for

pinky5thco@ 28 Aug 2015 01:28 PM## Thank you very much for

pinky5thco@ 28 Aug 2015 01:34 PM## Thanks for mour information

sonigupta@ 1 Sep 2015 12:29 PM## Thank you very much for

pinky5thco@ 3 Sep 2015 07:09 PM## Thanks for more information

sonigupta@ 8 Sep 2015 12:01 PM## Thanks for more information

sonigupta@ 8 Sep 2015 12:03 PM## Thanks for More information

josisonali@ 9 Sep 2015 12:13 PM