# European Girls' Mathematical Olympiad 2012-Day 2 Solutions Problem 5. The numbers $p$ and $q$ are prime and satisfy $$ \frac{p}{p+1}+\frac{q+1}{q}=\frac{2 n}{n+2} $$ for some positive integer $n$. Find all possible values of $q-p$. Origin. Luxembourg (Pierre Haas). Solution 1 (submitter). Rearranging the equation, $2 q n(p+1)=(n+2)(2 p q+p+q+1)$. The left hand side is even, so either $n+2$ or $p+q+1$ is even, so either $p=2$ or $q=2$ since $p$ and $q$ are prime, or $n$ is even. If $p=2,6 q n=(n+2)(5 q+3)$, so $(q-3)(n-10)=36$. Considering the divisors of 36 for which $q$ is prime, we find the possible solutions $(p, q, n)$ in this case are $(2,5,28)$ and $(2,7,19)$ (both of which satisfy the equation). If $q=2,4 n(p+1)=(n+2)(5 p+3)$, so $n=p n+10 p+6$, a contradiction since $n

1$ if and only if $q \mid p+1$. Since $(\ell, \ell+1)$, we see that, if $q \nmid p+1$, then $\ell=p q+p+1$ and $\ell+1=q(p+1)$, so $q=p+2$ (and $\left(p, p+2,2\left(2 p^{2}+6 p+3\right)\right)$ satisfies the original equation). In the contrary case, suppose $p+1=r q$, so $\ell(p+1)=(\ell+1)(p+r)$, a contradiction since $\ell<\ell+1$ and $p+1 \leq p+r$. Thus the possible values of $q-p$ are 2,3 and 5 . Solution 2 (PSC). Subtracting 2 and multiplying by -1 , the condition is equivalent to $$ \frac{1}{p+1}-\frac{1}{q}=\frac{4}{n+2} $$ Thus $q>p+1$. Rearranging, $$ q-p-1=\frac{4(p+1) q}{n+2} $$ The expression on the right is a positive integer, and $q$ must cancel into $n+2$ else $q$ would divide $p+1p+1$. Therefore $q$ and $p+1$ are coprime, since $q$ is prime. Group the terms on the LHS to get $$ \frac{q-p-1}{q(p+1)}=\frac{4}{n+2} $$ Now $(q, q-p-1)=(q, p+1)=1$ and $(p+1, q-p-1)=(p+1, q)=1$ so the fraction on the left is in lowest terms. Therefore the numerator must divide the numerator on the right, which is 4 . Since $q-p-1$ is positive, it must be 1,2 or 4 , so that $q-p$ must be 2,3 or 5 . All of these can be attained, by $(p, q, n)=(3,5,78)$, $(2,5,28)$ and $(2,7,19)$ respectively. Problem 6. There are infinitely many people registered on the social network Mugbook. Some pairs of (different) users are registered as friends, but each person has only finitely many friends. Every user has at least one friend. (Friendship is symmetric; that is, if $A$ is a friend of $B$, then $B$ is a friend of $A$.) Each person is required to designate one of their friends as their best friend. If $A$ designates $B$ as her best friend, then (unfortunately) it does not follow that $B$ necessarily designates $A$ as her best friend. Someone designated as a best friend is called a 1-best friend. More generally, if $n>1$ is a positive integer, then a user is an $n$-best friend provided that they have been designated the best friend of someone who is an $(n-1)$-best friend. Someone who is a $k$-best friend for every positive integer $k$ is called popular. (a) Prove that every popular person is the best friend of a popular person. (b) Show that if people can have infinitely many friends, then it is possible that a popular person is not the best friend of a popular person. Origin. Romania (Dan Schwarz) (rephrasing by Geoff Smith). Remark. The original formulation of this problem was: Given a function $f: X \rightarrow X$, let us use the notations $f^{0}(X):=X, f^{n+1}(X):=f\left(f^{n}(X)\right)$ for $n \geq 0$, and also $f^{\omega}(X):=\bigcap_{n \geq 0} f^{n}(X)$. Let us now impose on $f$ that all its fibres $f^{-1}(y):=\{x \in X \mid f(x)=y\}$, for $y \in f(X)$, are finite. Prove that $f\left(f^{\omega}(X)\right)=f^{\omega}(X)$. Solution 1 (submitter, adapted). For any person $A$, let $f^{0}(x)=x$, let $f(A)$ be $A$ 's best friend, and define $f^{k+1}(A)=f\left(f^{k}(A)\right)$, so any person who is a $k$-best friend is $f^{k}(A)$ for some person $A$; clearly a $k$-best friend is also an $\ell$-best friend for all $\ell2$, if $|U|=|V|$ it follows $U=V$, and so we can again take $W=U=V$. If not, assume without loss of generality $|U|<|V|$; then $V=U V^{\prime}$, so $U U V^{\prime}=U V^{\prime} U$, whence $U V^{\prime}=V^{\prime} U$. Since $\left|V^{\prime}\right|<|V|$, it follows $2 \leq|U|+\left|V^{\prime}\right|<|U|+|V|$, so by the induction hypothesis there exists a suitable word $W$ such that $U=W^{|U| /|W|}, V^{\prime}=W^{\left|V^{\prime}\right| /|W|}$, so $V=U V^{\prime}=W^{|U| /|W|} W^{\left|V^{\prime}\right| /|W|}=W^{\left(|U|+\left|V^{\prime}\right|\right) /|W|}=W^{|V| /|W|}$. Now, assuming without loss of generality $p \leq q, q=k p+r$, we have $N=Q P S$, with $|Q|=q,|P|=p$. If $r=0$ all is clear; otherwise it follows we can write $P=U V, Q=V(U V)^{k}$, with $|V|=r$, whence $U V=V U$, implying $P Q=Q P$, and so by the above result there will exist a word $W$ of length $\operatorname{gcd}(p, q)$ such that $P=W^{p / \operatorname{gcd}(p, q)}$, $Q=W^{q / \operatorname{gcd}(p, q)}$, therefore $N$ is $\operatorname{gcd}(p, q)$-periodic. By this we need $|C A| \leq p+q-1$, hence $w \leq p+q+2$, otherwise by the previous lemma $W_{0}^{\prime}$ and $W_{0}^{\prime \prime}$ would be identical, absurd. Since $p \mid w$ and $11$, the first word is made up of $k b$ repetitions of the subword of length ga and the second word is made up of ka repetitions of the subword of length gb . These two words have distance at least $\max (k a, k b)$. Proof. We may assume $k=1$, since the distance between the words is $k$ times the distance between their initial subwords of length $g a b$. Without loss of generality suppose $b>a$. For each positive integer $m$, look at the subsequence in each word of letters in positions congruent to $m$ $(\bmod g)$. Those subsequences (of length $a b)$ have periods dividing $a$ and $b$ respectively. If they are equal, then they are constant (since each letter is equal to those $a$ and $b$ before and after it, mod $a b$, and $(a, b)=1$ ). Because $a>1$, there is some $m$ for which the first subsequence is not constant, and so is unequal to the second subsequence. Restrict attention to those subsequences. We now have two distinct repetitive words, one (nonconstant) made up of $b$ repetitions of a subword of length $a$ and one made up of $a$ repetitions of a subword of length $b$. Looking at the first of those words, for any $1 \leq t \leq b$ consider the letters in positions $t, t+b, \ldots, t+(a-1) b$. These letters cover every position (mod $a)$; since the first word is not constant, the letters are not all equal, but the letters in the corresponding positions in the second word are all equal. At least one of these letters in the first word must change to make them all equal to those in the corresponding positions in the second word; repeating for each $t$, at least $b$ letters must change, so the words have distance at least $b$. In the original problem, consider all the words (which we suppose to be repetitive) obtained by a transposition of two adjacent letters from the original nonconstant word; say that word has length $n$. Suppose those words include two distinct words with periods $n / a$ and $n / b$; those words have distance at most 4 . If $a>4$ or $b>4$, we have a contradiction unless $a \mid b$ or $b \mid a$. If $a>4$ is the greatest number of repetitions in any of the words ( $n / a$ is the smallest period), then unless all the numbers of repetitions divide each other there must be words with 2 or 4 repetitions, words with 3 repetitions and all larger numbers of repetitions must divide each other and be divisible by 6 . We now divide into three cases: all the numbers of repetitions may divide either other; or there may be words with (multiples of) 2,3 and 6 repetitions; or all words may have at most 4 repetitions, with at least one word having 3 repetitions and at least one having 2 or 4 repetitions. Case 1. Suppose all the numbers of repetitions divide each other. Let $k$ be the least number of repetitions. Consider the word as being divided into $k$ blocks, each of $\ell$ letters; any transposition of two adjacent letters leaves those blocks identical. If any two adjacent letters within a block are the same, then this means all the blocks are already identical; since the word is not constant, the letters in the first block are not all identical, so there are two distinct adjacent letters in the first block, and transposing them leaves it distinct from the other blocks, a contradiction. Otherwise, all pairs of adjacent letters within each block are distinct; transposing any adjacent pair within the first block leaves it identical to the second block. If the first block has more than two letters, this is impossible since transposing the first two letters has a different result from transposing the second two. So the blocks all have length 2 ; similarly, there are just two blocks, the arrangement is $a b b a$ but transposing the adjacent letters $b b$ does not leave the word repetitive. Case 2. Suppose some word resulting from a transposition is made of (a multiple of) 6 repetitions, some of 3 repetitions and some of 2 repetitions (or 4 repetitions, counted as 2 ). Consider it as a sequence of 6 blocks, each of length $\ell$. If the six blocks are already identical, then as the word is not constant, there are some two distinct adjacent letters within the first block; transposing them leaves a result where the blocks form a pattern $B A A A A A$, which cannot have two, three or six repetitions. So the six blocks are not already identical. If a transposition within a block results in them being identical, the blocks form a pattern (without loss of generality) $B A A A A A, A B A A A A$ or $A A B A A A$. In any of these cases, apply the same transposition (that converts between $A$ and $B$ ) to an $A$ block adjacent to the $B$ block, and the result cannot have two, three or six repetitions. Finally, consider the case where some transposition between two adjacent blocks results in all six blocks being identical. The patterns are $B C A A A A, A B C A A A$ and $A A B C A A$ (and considering the letters at the start and end of each block shows $B \neq C$ ). In all cases, transposing two adjacent distinct letters within an $A$ block produces a result that cannot have two, three or six repetitions. Case 3. In the remaining case, all words have at most 4 repetitions, at least one has 3 repetitions and at least one has 2 or 4 repetitions. For the purposes of this case we will think of 4 -repetition words as being 2 -repetition words. The number of each letter is a multiple of 6 , so $n \geq 12$; consider the word as made of six blocks of length $\ell$. If the word is already repetitive with 2 repetitions, pattern $A B C A B C$, any transposition between two distinct letters leaves it no longer repetitive with two repetitions, so it must instead have three repetitions after the transposition. If $A B$ is not all one letter, transposing two adjacent letters within $A B$ implies that $C A=B C$, so $A=B=C$, the word has pattern $A A A A A A$ but transposing within the initial $A A$ means it no longer has 3 repetitions. This implies that $A B$ is all one letter, but similarly $B C$ must also be all one letter and so the word is constant, a contradiction. If the word is already repetitive with 3 repetitions, it has pattern $A B A B A B$ and any transposition leaves it no longer having 3 repetitions, so having 2 repetitions instead. $A B A$ is not made all of one letter (since the word is not constant) and any transposition between two adjacent distinct letters therein turns it into $B A B$; such a transposition affects at most two of the blocks, so $A=B$, the word has pattern $A A A A A A$ and transposing two adjacent distinct letters within the first half cannot leave it with two repetitions. So the word is not already repetitive, and so no two adjacent letters are the same; all transpositions give distinct strings. Consider transpositions of adjacent letters within the first four letters; three different words result, of which at most one is periodic with two repetitions (it must be made of two copies of the second half of the word) and at most one is periodic with three repetitions, a contradiction.