An L-expression is defined as any one of the following:

- a natural number;

- the symbol $A$;

- the symbol $Z$;

- the symbol $S$;

- a pair of L-expressions $u, v$, which is written as $u(v)$.

An L-expression can be transformed according to the following rules:

- $A(x) \to x + 1$ for any natural number $x$;

- $Z(u)(v) \to v$ for any L-expressions $u, v$;

- $S(u)(v)(w) \to v(u(v)(w))$ for any L-expressions $u, v, w$.

For example, after applying all possible rules, the L-expression $S(Z)(A)(0)$ is transformed to the number $1$:
$$S(Z)(A)(0) \to A(Z(A)(0)) \to A(0) \to 1.$$
Similarly, the L-expression $S(S)(S(S))(S(Z))(A)(0)$ is transformed to the number $6$ after applying all possible rules.

Find the result of the L-expression $S(S)(S(S))(S(S))(S(Z))(A)(0)$ after applying all possible rules. Give the last nine digits as your answer.

Note: it can be proved that the L-expression in question can only be transformed a finite number of times, and the final result does not depend on the order of the transformations.

Answer: 399885292


An L-expression is defined as any one of the following:

- a natural number;

- the symbol $A$;

- the symbol $Z$;

- the symbol $S$;

- a pair of L-expressions $u, v$, which is written as $u(v)$.

An L-expression can be transformed according to the following rules:

- $A(x) \to x + 1$ for any natural number $x$;

- $Z(u)(v) \to v$ for any L-expressions $u, v$;

- $S(u)(v)(w) \to v(u(v)(w))$ for any L-expressions $u, v, w$.

For example, after applying all possible rules, the L-expression $S(Z)(A)(0)$ is transformed to the number $1$:
$$S(Z)(A)(0) \to A(Z(A)(0)) \to A(0) \to 1.$$
Similarly, the L-expression $S(S)(S(S))(S(Z))(A)(0)$ is transformed to the number $6$ after applying all possible rules.

Define the following L-expressions:

- $C_0 = Z$;

- $C_i = S(C_{i - 1})$ for $i \ge 1$;

- $D_i = C_i(S)(S)$.

For natural numbers $a, b, c, d, e$, let $F(a, b, c, d, e)$ denote the result of the L-expression $D_a(D_b)(D_c)(C_d)(A)(e)$ after applying all possible rules.

Find the last nine digits of $F(12, 345678, 9012345, 678, 90)$.

Note: it can be proved that the L-expression in question can only be transformed a finite number of times, and the final result does not depend on the order of the transformations.

Answer: 547480666


An irrational number $x$ can be uniquely expressed as a continued fraction $[a_0; a_1,a_2,a_3,\dots]$:
$$
x=a_{0}+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+{_\ddots}}}}
$$where $a_0$ is an integer and $a_1,a_2,a_3,\dots$ are positive integers.

Define $k_j(x)$ to be the geometric mean of $a_1,a_2,\dots,a_j$.
That is, $k_j(x)=(a_1a_2 \cdots a_j)^{1/j}$.
Also define $k_\infty(x)=\lim_{j\to \infty} k_j(x)$.

Khinchin proved that almost all irrational numbers $x$ have the same value of $k_\infty(x)\approx2.685452\dots$ known as Khinchin's constant. However, there are some exceptions to this rule.

For $n\geq 0$ define
$$\rho_n = \sum_{i=0}^{\infty} \frac{2^n}{2^{2^i}}
$$For example $\rho_2$, with continued fraction beginning $[3; 3, 1, 3, 4, 3, 1, 3,\dots]$, has $k_\infty(\rho_2)\approx2.059767$.

Find the geometric mean of $k_{\infty}(\rho_n)$ for $0\leq n\leq 50$, giving your answer rounded to six digits after the decimal point.

Answer: 5679.934966


Let $s_n$ be the $n$-th positive integer that does not contain three consecutive ones in its binary representation.

For example, $s_1 = 1$ and $s_7 = 8$.

Define $F(N)$ to be the sum of $n^2$ for all $n\leq N$ where $s_n$ is odd. You are given $F(10)=199$.

Find $F(10^{16})$ giving your answer modulo $10^9+7$.

Answer: 674045136


The numbers from $1$ to $12$ can be arranged into a $3 \times 4$ matrix in either row-major or column-major order:
$$R=\begin{pmatrix}
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
9 & 10 & 11 & 12\end{pmatrix}, C=\begin{pmatrix}
1 & 4 & 7 & 10\\
2 & 5 & 8 & 11\\
3 & 6 & 9 & 12\end{pmatrix}$$
By swapping two entries at a time, at least $8$ swaps are needed to transform $R$ to $C$.

Let $S(n, m)$ be the minimal number of swaps needed to transform an $n\times m$ matrix of $1$ to $nm$ from row-major order to column-major order. Thus $S(3, 4) = 8$.

You are given that the sum of $S(n, m)$ for $2 \leq n \leq m \leq 100$ is $12578833$.

Find the sum of $S(n^4, m^4)$ for $2 \leq n \leq m \leq 100$.

Answer: 2101925115560555020


For a given integer $R$ consider all primitive Pythagorean triangles that can fit inside, without touching, a circle with radius $R$. Define $F(R)$ to be the largest inradius of those triangles. You are given $F(100) = 36$.

Find $F(10^{18})$.

Answer: 414213562371805310


The function $s(n)$ is defined recursively for positive integers by
$s(1) = 1$ and $s(n+1) = \big(s(n) - 1\big)^3 +2$ for $n\geq 1$.

The sequence begins: $s(1) = 1, s(2) = 2, s(3) = 3, s(4) = 10, \ldots$.

For positive integers $N$, define $$T(N) = \sum_{a=1}^N \sum_{b=1}^N \gcd\Big(s\big(s(a)\big), s\big(s(b)\big)\Big).$$  You are given $T(3) = 12$, $T(4) \equiv 24881925$ and $T(100)\equiv 14416749$ both modulo $123456789$.

Find $T(10^8)$. Give your answer modulo $123456789$.

Answer: 55601924


Let $P(n)$ be the number of permutations of $\{1,2,3,\ldots,2n\}$ such that:

1. There is no ascending subsequence with more than $n+1$ elements, and

2. There is no descending subsequence with more than two elements.

Note that subsequences need not be contiguous. For example, the permutation $(4,1,3,2)$ is not counted because it has a descending subsequence of three elements: $(4,3,2)$. You are given $P(2)=13$ and $P(10) \equiv 45265702 \pmod{10^9 + 7}$.

Find $P(10^8)$ and give your answer modulo $10^9 + 7$.

Answer: 877789135


The sequence $s_n$ is defined by $s_1 = 102022661$ and $s_n = s_{n-1}^2 \bmod {998388889}$ for $n > 1$.

Let $a_n = s_{2n - 1}$ and $b_n = s_{2n}$ for $n=1,2,...$

Define an $N \times N$ matrix whose values are $M_{i,j} = a_i + b_j$.

Let $A(N)$ be the minimal path sum from $M_{1,1}$ (top left) to $M_{N,N}$ (bottom right), where each step is either right or down.

You are given $A(1) = 966774091$, $A(2) = 2388327490$ and $A(10) = 13389278727$.

Find $A(10^7)$.

Answer: 9986212680734636


The sequence $a_n$ is defined by $a_1=1$, and then recursively for $n\geq1$:
$$\begin{align*}
a_{2n}  &=2a_n\\
a_{2n+1} &=a_n-3a_{n+1}
\end{align*}$$
The first ten terms are $1, 2, -5, 4, 17, -10, -17, 8, -47, 34$.

Define $\displaystyle S(N) = \sum_{n=1}^N a_n$. You are given $S(10) = -13$.

Find $S(10^{12})$.

Answer: -6999033352333308


We call a triangle fortunate if it has integral sides and at least one of its vertices has the property that the distance from it to the triangle's orthocentre is exactly half the distance from the same vertex to the triangle's circumcentre.

Triangle $ABC$ above is an example of a fortunate triangle with sides $(6,7,8)$. The distance from the vertex $C$ to the circumcentre $O$ is $\approx 4.131182$, while the distance from $C$ to the orthocentre $H$ is half that, at $\approx 2.065591$.

Define $S(P)$ to be the sum of $a+b+c$ over all fortunate triangles with sides $a\leq b\leq c$ and perimeter not exceeding $P$.

For example $S(10)=24$, arising from three triangles with sides $(1,2,2)$, $(2,3,4)$, and $(2,4,4)$. You are also given $S(100)=3331$.

Find $S(10^7)$.

Answer: 134222859969633


For a positive integer $n$ we define $\tau(n)$ to be the count of the divisors of $n$. For example, the divisors of $12$ are $\{1,2,3,4,6,12\}$ and so $\tau(12) = 6$.

A positive integer $n$ is a tau number if it is divisible by $\tau(n)$. For example $\tau(12)=6$ and $6$ divides $12$ so $12$ is a tau number.

Let $m(k)$ be the smallest tau number $x$ such that $\tau(x) = k$. For example, $m(8) = 24$, $m(12)=60$ and $m(16)=384$.

Further define $M(n)$ to be the sum of all $m(k)$ whose values do not exceed $10^n$. You are given $M(3) = 3189$.

Find $M(16)$.

Answer: 1154027691000533893


Consider the following recurrence relation:
$$\begin{align}
a_0 &= \frac{\sqrt 5 + 1}2\\
a_{n+1} &= \dfrac{a_n(a_n^4 + 10a_n^2 + 5)}{5a_n^4 + 10a_n^2 + 1}
\end{align}$$

Note that $a_0$ is the golden ratio.

$a_n$ can always be written in the form $\dfrac{p_n\sqrt{5}+1}{q_n}$, where $p_n$ and $q_n$ are positive integers.

Let $s(n)=p_n^5+q_n^5$. So, $s(0)=1^5+2^5=33$.

The Fibonacci sequence is defined as: $F_1=1$, $F_2=1$, $F_n=F_{n-1}+F_{n-2}$ for $n > 2$.

Define $\displaystyle S(m)=\sum_{i=2}^{m}s(F_i)$.

Find $S(1618034)$. Submit your answer modulo $398874989$.

Answer: 378401935


A Young diagram is a finite collection of (equally-sized) squares in a grid-like arrangement of rows and columns, such that

- the left-most squares of all rows are aligned vertically;

- the top squares of all columns are aligned horizontally;

- the rows are non-increasing in size as we move top to bottom;

- the columns are non-increasing in size as we move left to right.

Two examples of Young diagrams are shown below.

Two players Right and Down play a game on several Young diagrams, all disconnected from each other. Initially, a token is placed in the top-left square of each diagram. Then they take alternating turns, starting with Right. On Right's turn, Right selects a token on one diagram and moves it any number of squares to the right. On Down's turn, Down selects a token on one diagram and moves it any number of squares downwards. A player unable to make a legal move on their turn loses the game.

For $a,b,k\geq 1$ we define an $(a,b,k)$-staircase to be the Young diagram where the bottom-right frontier consists of $k$ steps of vertical height $a$ and horizontal length $b$. Shown below are four examples of staircases with $(a,b,k)$ respectively $(1,1,4),$ $(5,1,1),$ $(3,3,2),$ $(2,4,3)$.

Additionally, define the weight of an $(a,b,k)$-staircase to be $a+b+k$.

Let $R(m, w)$ be the number ways of choosing $m$ staircases, each having weight not exceeding $w$, upon which Right (moving first in the game) will win the game assuming optimal play. Different orderings of the same set of staircases are to be counted separately.

For example, $R(2, 4)=7$ is illustrated below, with tokens as grey circles drawn in their initial positions.

You are also given $R(3, 9)=314104$.

Find $R(8, 64)$ giving your answer modulo $10^9+7$.

Answer: 858945298


A Young diagram is a finite collection of (equally-sized) squares in a grid-like arrangement of rows and columns, such that

- the left-most squares of all rows are aligned vertically;

- the top squares of all columns are aligned horizontally;

- the rows are non-increasing in size as we move top to bottom;

- the columns are non-increasing in size as we move left to right.

Two examples of Young diagrams are shown below.

Two players Right and Down play a game on several Young diagrams, all disconnected from each other. Initially, a token is placed in the top-left square of each diagram. Then they take alternating turns, starting with Right. On Right's turn, Right selects a token on one diagram and moves it one square to the right. On Down's turn, Down selects a token on one diagram and moves it one square downwards. A player unable to make a legal move on their turn loses the game.

For $a,b,k\geq 1$ we define an $(a,b,k)$-staircase to be the Young diagram where the bottom-right frontier consists of $k$ steps of vertical height $a$ and horizontal length $b$. Shown below are four examples of staircases with $(a,b,k)$ respectively $(1,1,4),$ $(5,1,1),$ $(3,3,2),$ $(2,4,3)$.

Additionally, define the weight of an $(a,b,k)$-staircase to be $a+b+k$.

Let $S(m, w)$ be the number ways of choosing $m$ staircases, each having weight not exceeding $w$, upon which Right (moving first in the game) will win the game assuming optimal play. Different orderings of the same set of staircases are to be counted separately.

For example, $S(2, 4)=7$ is illustrated below, with tokens as grey circles drawn in their initial positions.

You are also given $S(3, 9)=315319$.

Find $S(8, 64)$ giving your answer modulo $10^9+7$.

Answer: 740759929


Let $B(n)$ be the smallest number larger than $n$ that can be formed by rearranging digits of $n$, or $0$ if no such number exists. For example, $B(245) = 254$ and $B(542) = 0$.

Define $a_0 = 0$ and $a_n = a_{n - 1}^2 + 2$ for $n>0$.
Let $\displaystyle U(N) = \sum_{n = 1}^N B(a_n)$. You are given $U(10) \equiv 543870437 \pmod{10^9+7}$.

Find $U(10^{16})$. Give your answer modulo $10^9 + 7$.

Answer: 811141860


Let $B(n)$ be the smallest number larger than $n$ that can be formed by rearranging digits of $n$, or $0$ if no such number exists. For example, $B(245) = 254$ and $B(542) = 0$.

Define $\displaystyle T(N) = \sum_{n=1}^N B(n^2)$. You are given $T(10)=270$ and $T(100)=335316$.

Find $T(10^{16})$. Give your answer modulo $10^9 + 7$.

Answer: 400034379


A round number is a number that ends with one or more zeros in a given base.

Let us define the roundness of a number $n$ in base $b$ as the number of zeros at the end of the base $b$ representation of $n$.

For example, $20$ has roundness $2$ in base $2$, because the base $2$ representation of $20$ is $10100$, which ends with $2$ zeros.

Also define $R(n)$, the total roundness of a number $n$, as the sum of the roundness of $n$ in base $b$ for all $b > 1$.

For example, $20$ has roundness $2$ in base $2$ and roundness $1$ in base $4$, $5$, $10$, $20$, hence we get $R(20)=6$.

You are also given $R(10!) = 312$.

Find $R(10\,000\,000!)$. Give your answer modulo $10^9 + 7$.

Answer: 40410219


A full $k$-ary tree is a tree with a single root node, such that every node is either a leaf or has exactly $k$ ordered children.  The height of a $k$-ary tree is the number of edges in the longest path from the root to a leaf.

For instance, there is one full 3-ary tree of height 0, one full 3-ary tree of height 1, and seven full 3-ary trees of height 2. These seven are shown below.

For integers $n$ and $k$ with $n\ge 0$ and $k \ge 2$, define $t_k(n)$ to be the number of full $k$-ary trees of height $n$ or less.

Thus, $t_3(0) = 1$, $t_3(1) = 2$, and $t_3(2) = 9$. Also, $t_2(0) = 1$, $t_2(1) = 2$, and $t_2(2) = 5$.

Define $S_k$ to be the set of positive integers $m$ such that $m$ divides $t_k(n)$ for some integer $n\ge 0$.  For instance, the above values show that 1, 2, and 5 are in $S_2$ and 1, 2, 3, and 9 are in $S_3$.

Let $S = \bigcap_p S_p$ where the intersection is taken over all primes $p$.  Finally, define $R(N)$ to be the sum of all elements of $S$ not exceeding $N$.  You are given that $R(20) = 18$ and $R(1000) = 2089$.

Find $R(10^7)$.

Answer: 207282955


This problem is based on (but not identical to) the scoring for the card game
Cribbage.

Consider a normal pack of $52$ cards. A Hand is a selection of one or more of these cards.

For each Hand the Hand score is the sum of the values of the cards in the Hand where the value of Aces is $1$ and the value of court cards (Jack, Queen, King) is $10$.

The Cribbage score is obtained for a Hand by adding together the scores for:

-
Pairs. A pair is two cards of the same rank. Every pair is worth $2$ points.

-
Runs. A run is a set of at least $3$ cards whose ranks are consecutive, e.g. 9, 10, Jack. Note that Ace is never high, so Queen, King, Ace is not a valid run. The number of points for each run is the size of the run. All locally maximum runs are counted. For example, 2, 3, 4, 5, 7, 8, 9 the two runs of 2, 3, 4, 5 and 7, 8, 9 are counted but not 2, 3, 4 or 3, 4, 5.

-
Fifteens. A fifteen is a combination of cards that has value adding to $15$. Every fifteen is worth $2$ points. For this purpose the value of the cards is the same as in the Hand Score.

For example, $(5 \spadesuit, 5 \clubsuit, 5 \diamondsuit, K \heartsuit)$ has a Cribbage score of $14$ as there are four ways that fifteen can be made and also three pairs can be made.

The example $( A \diamondsuit, A \heartsuit, 2 \clubsuit, 3 \heartsuit, 4 \clubsuit, 5 \spadesuit)$ has a Cribbage score of $16$: two runs of five worth $10$ points, two ways of getting fifteen worth $4$ points and one pair worth $2$ points. In this example the Hand score is equal to the Cribbage score.

Find the number of Hands in a normal pack of cards where the Hand score is equal to the Cribbage score.

Answer: 81108001093


A composition of $n$ is a sequence of positive integers which sum to $n$. Such a sequence can be split into runs, where a run is a maximal contiguous subsequence of equal terms.

For example, $2,2,1,1,1,3,2,2$ is a composition of $14$ consisting of four runs:

$2, 2\quad 1, 1, 1\quad 3 \quad 2, 2$

Let $F(n)$ be the number of compositions of $n$ where every run has odd length.

For example, $F(5)=10$:

$$\begin{align*}
& 5 &&4,1  && 3,2 &&2,3 &&2,1,2\\
&2,1,1,1 &&1,4 &&1,3,1 &&1,1,1,2 &&1,1,1,1,1
\end{align*}$$
Find $F(10^5)$. Give your answer modulo $1111124111$.

Answer: 57322484


Given $n\ge 2$ bowls arranged in a circle, $m\ge 2$ balls are distributed amongst them.

Initially the balls are distributed randomly: for each ball, a bowl is chosen equiprobably and independently of the other balls. After this is done, we start the following process:

- Choose one of the $m$ balls equiprobably at random.

- Choose a direction to move - either clockwise or anticlockwise - again equiprobably at random.

- Move the chosen ball to the neighbouring bowl in the chosen direction.

- Return to step 1.

This process stops when all the $m$ balls are located in the same bowl. Note that this may be after zero steps, if the balls happen to have been initially distributed all in the same bowl.

Let $F(n, m)$ be the expected number of times we move a ball before the process stops. For example, $F(2, 2) = \frac{1}{2}$, $F(3, 2) = \frac{4}{3}$, $F(2, 3) = \frac{9}{4}$, and $F(4, 5) = \frac{6875}{24}$.

Let $G(N, M) = \sum_{n=2}^N \sum_{m=2}^M F(n, m)$. For example, $G(3, 3) = \frac{137}{12}$ and $G(4, 5) = \frac{6277}{12}$. You are also given that $G(6, 6) \approx 1.681521567954e4$ in scientific format with 12 significant digits after the decimal point.

Find $G(12, 12)$. Give your answer in scientific format with 12 significant digits after the decimal point.

Answer: 1.345679959251e12


For a positive integer $n$ construct a graph using all the divisors of $n$ as the vertices. An edge is drawn between $a$ and $b$ if $a$ is divisible by $b$ and $a/b$ is prime, and is given weight $\phi(a)-\phi(b)$, where $\phi$ is the Euler totient function.
Define $t(n)$ to be the total weight of this graph.

The example below shows that $t(45) = 52$

Let $T(N)=\displaystyle\sum_{n=1}^{N} t(n)$. You are given $T(10)=26$ and $T(10^2)=5282$.

Find $T(10^{12})$. Give your answer modulo $715827883$.

Answer: 128856311


For the year $2025$

$$2025 = (20 + 25)^2$$
Given positive integers $a$ and $b$, the concatenation $ab$ we call a $2025$-number if $ab = (a+b)^2$.

Other examples are $3025$ and $81$.

Note $9801$ is not a $2025$-number because the concatenation of $98$ and $1$ is $981$.

Let $T(n)$ be the sum of all $2025$-numbers with $n$ digits or less. You are given $T(4) = 5131$.

Find $T(16)$.

Answer: 72673459417881349


Starting with one piece of integer-sized rectangle paper, two players make moves in turn.

A valid move consists of choosing one piece of paper and cutting it both horizontally and vertically, so that it becomes four pieces of smaller rectangle papers, all of which are integer-sized.

The player that does not have a valid move loses the game.

Let $C(w, h)$ be the number of winning moves for the first player, when the original paper has size $w \times h$. For example, $C(5,3)=4$, with the four winning moves shown below.

Also write $\displaystyle D(W, H) = \sum_{w = 2}^W\sum_{h = 2}^H C(w, h)$. You are given that $D(12, 123) = 327398$.

Find $D(123, 1234567)$.

Answer: 5707485980743099


We define the unlucky prime of a number $n$, denoted $u(n)$, as the smallest prime number $p$ such that the remainder of $n$ divided by $p$ (i.e. $n \bmod p$) is not a multiple of seven.

For example, $u(14) = 3$, $u(147) = 2$ and $u(1470) = 13$.

Let $U(N)$ be the sum $\sum_{n = 1}^N u(n)$.

You are given $U(1470) = 4293$.

Find $U(10^{17})$.

Answer: 292137809490441370


A square of side length $b<1$ is rolling around the inside of a larger square of side length $1$, always touching the larger square but without sliding.

Initially the two squares share a common corner. At each step, the small square rotates clockwise about a corner that touches the large square, until another of its corners touches the large square. Here is an illustration of the first three steps for $b = \frac5{13}$.

For some values of $b$, the small square may return to its initial position after several steps. For example, when $b = \frac12$, this happens in $4$ steps; and for $b = \frac5{13}$ it happens in $24$ steps.

Let $F(N)$ be the number of different values of $b$ for which the small square first returns to its initial position within at most $N$ steps. For example, $F(6) = 4$, with the corresponding $b$ values:
$$\frac12,\quad 2 - \sqrt 2,\quad 2 + \sqrt 2 - \sqrt{2 + 4\sqrt2},\quad  8 - 5\sqrt2 + 4\sqrt3 - 3\sqrt6,$$
the first three in $4$ steps and the last one in $6$ steps. Note that it does not matter whether the small square returns to its original orientation.

Also $F(100) = 805$.

Find $F(10^8)$.

Answer: 759908921637225


A peerless tree is a tree with no edge between two vertices of the same degree. Let $P(n)$ be the number of peerless trees on $n$ unlabelled vertices.

There are six of these trees on seven unlabelled vertices, $P(7)=6$, shown below.

Define $\displaystyle S(N)  = \sum_{n=3}^N P(n)$. You are given $S(10) = 74$.

Find $S(50)$.

Answer: 12144907797522336


Let $\theta=\sqrt{-2}$.

Define $T$ to be the set of numbers of the form $a+b\theta$, where $a$ and $b$ are integers and either $a\gt 0$, or $a=0$ and $b\gt 0$. For a set $S \subseteq T$ and element $z \in T$, define $p(S,z)$ to be the number of ways of choosing two distinct elements from $S$ with product either $z$ or $-z$.

For example if $S=\{1,2,4\}$ and $z=4$, there is only one valid pair of elements with product $\pm4$, namely $1$ and $4$. Thus, in this case $p(S,z)=1$.

For another example, if $S=\{1,\theta,1+\theta,2-\theta\}$ and $z=2-\theta$, we have $1\cdot(2-\theta)=z$ and $\theta\cdot(1+\theta)=-z$, giving $p(S,z)=2$.

Let $A$ and $B$ be two sets satisfying the following conditions:

- $1 \in A$

- $A \cap B = \emptyset$

- $A \cup B = T$

- $p(A,z) = p(B,z)$ for all $z\in T$

Remarkably, these four conditions uniquely determine the sets $A$ and $B$.

Let $F_n$ be the set of the first $n$ factorials: $F_n=\{1!,2!,\dots,n!\}$, and define $G(n)$ to be the sum of all elements of $F_n\cap A$.

You are given $G(4) = 25$, $G(7) = 745$, and $G(100) \equiv 709772949 \pmod{10^9+7}$.

Find $G(10^8)$ and give your answer modulo $10^9+7$.

Answer: 792169346


A deck of cards contains $R$ red cards and $B$ black cards.

A card is chosen uniformly randomly from the deck and removed. A second card is then chosen uniformly randomly from the cards remaining and removed.

-
If both cards are red, they are discarded.

-
If both cards are black, they are both put back in the deck.

-
If they are different colours, the red card is put back in the deck and the black card is discarded.

Play ends when all the remaining cards in the deck are the same colour and let $P(R,B)$ be the probability that this colour is black.

You are given $P(2,2) = 0.4666666667$, $P(10,9) = 0.4118903397$ and $P(34,25) = 0.3665688069$.

Find $P(24690,12345)$. Give your answer with 10 digits after the decimal point.

Answer: 0.2928967987


Two players A and B are playing a variant of Nim.

At the beginning, there are several piles of stones. Each pile is either at the side of A or at the side of B. The piles are unordered.

They make moves in turn. At a player's turn, the player can

- either choose a pile on the opponent's side and remove one stone from that pile;

- or choose a pile on their own side and remove the whole pile.

The winner is the player who removes the last stone.

Let $E(N)$ be the number of initial settings with at most $N$ stones such that, whoever plays first, A always has a winning strategy.

For example $E(4) = 9$; the settings are:

| Nr. | Piles at the side of A | Piles at the side of B |
| --- | --- | --- |
| 1 | $4$ | none |
| 2 | $1, 3$ | none |
| 3 | $2, 2$ | none |
| 4 | $1, 1, 2$ | none |
| 5 | $3$ | $1$ |
| 6 | $1, 2$ | $1$ |
| 7 | $2$ | $1, 1$ |
| 8 | $3$ | none |
| 9 | $2$ | none |

Find $E(5000) \bmod 1234567891$.

Answer: 246776732


The Fibonacci sequence $(f_i)$ is the unique sequence such that

- $f_0=0$

- $f_1=1$

- $f_{i+1}=f_i+f_{i-1}$

Similarly, there is a unique function $A(m,n)$ such that

- $A(0,0)=0$

- $A(0,1)=1$

- $A(m+1,n)=A(m,n+1)+A(m,n)$

- $A(m+1,n+1)=2A(m+1,n)+A(m,n)$

Define $S(k)=\displaystyle\sum_{i=2}^k\sum_{j=2}^k A(f_i,f_j)$. For example
$$
\begin{align}
S(3)&=A(1,1)+A(1,2)+A(2,1)+A(2,2)\\
&=2+5+7+16\\
&=30
\end{align}
$$You are also given $S(5)=10396$.

Find $S(50)$, giving your answer modulo $1123581313$.

Answer: 969134784


de Bruijn has a digital combination lock with $k$ buttons numbered $0$ to $k-1$ where $k \le 10$.

The lock opens when the last $n$ buttons pressed match the preset combination.

Unfortunately he has forgotten the combination. He creates a sequence of these digits which contains every possible combination of length $n$. Then by pressing the buttons in this order he is sure to open the lock.

Consider all sequences of shortest possible length that contains every possible combination of the digits.

Denote by $C(k, n)$ the lexicographically smallest of these.

For example, $C(3, 2) = $ 0010211220.

Define the sequence $a_n$ by $a_0=0$ and

$$a_n=(920461 a_{n-1}+800217387569)\bmod 10^{12} \text{ for }\  n > 0$$
Interpret each $a_n$ as a $12$-digit combination, adding leading zeros for any $a_n$ with less than $12$ digits.

Given a positive integer $N$, we are interested in the order the combinations $a_1,\dots,a_N$ appear in $C(10,12)$.

Denote by $p_n$ the place, numbered $1,\dots,N$, in which $a_n$ appears out of $a_1,\dots,a_N$. Define $\displaystyle F(N)=\sum_{n=1}^Np_na_n$.

For example, the combination $a_1=800217387569$ is entered before $a_2=696996536878$. Therefore:
$$F(2)=1\cdot800217387569 + 2\cdot696996536878 = 2194210461325$$
You are also given $F(10)=32698850376317$.

Find $F(10^7)$. Give your answer modulo $1234567891$.

Answer: 1068765750


Given a natural number $q$, let $p = 2^q - 1$ be the $q$-th Mersenne number.

Let $R(q)$ be the minimal square root of $q$ modulo $p$, if one exists. In other words, $R(q)$ is the smallest positive integer $x$ such that $x^2 - q$ is divisible by $p$.

For example, $R(5)=6$ and $R(17)=47569$.

Find $R(74\,207\,281)$. Give your answer modulo $10^9 + 7$.

Note: $2^{74207281}-1$ is prime.

Answer: 557539756


Given two unequal positive integers $a$ and $b$, we define a self-describing sequence consisting of alternating runs of $a$s and $b$s. The first element is $a$ and the sequence of run lengths is the original sequence.

For $a=2, b=3$, the sequence is:
$$2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 3, 2, 2, 2, 3, 3, 3,...$$
The sequence begins with two $2$s and two $3$s, then three $2$s and three $3$s, so the run lengths $2, 2, 3, 3, ...$ are given by the original sequence.

Let $T(a, b, N)$ be the sum of the first $N$ elements of the sequence. You are given $T(2,3,10) = 25$, $T(4,2,10^4) = 30004$, $T(5,8,10^6) = 6499871$.

Find $\sum T(a, b, 22332223332233)$ for $2 \le a \le 223$, $2 \le b \le 223$ and $a \neq b$. Give your answer modulo $2233222333$.

Answer: 1038733707


Given a set $E$ of positive integers, an element $x$ of $E$ is called an element divisor (elevisor) of $E$ if $x$ divides another element of $E$.

The sum of all elevisors of $E$ is denoted $\operatorname{sev}(E)$.

For example, $\operatorname{sev}(\{1, 2, 5, 6\}) = 1 + 2 = 3$.

Let $S(n)$ be the sum of $\operatorname{sev}(E)$ for all subsets $E$ of $\{1, 2, \dots, n\}$.

You are given $S(10) = 4927$.

Find $S(10^{14}) \bmod 1234567891$.

Answer: 1228599511


We use $x\oplus y$ for the bitwise XOR of $x$ and $y$.

Define the XOR-product of $x$ and $y$, denoted by $x \otimes y$, similar to a long multiplication in base $2$, except that the intermediate results are XORed instead of the usual integer addition.

For example, $7 \otimes 3 = 9$, or in base $2$, $111_2 \otimes 11_2 = 1001_2$:

$$\begin{align*}
\phantom{\otimes 111} 111_2 \\
\otimes \phantom{1111} 11_2 \\
\hline
\phantom{\otimes 111} 111_2 \\
\oplus \phantom{11} 111_2  \phantom{9} \\
\hline
\phantom{\otimes 11} 1001_2 \\
\end{align*}$$

We consider the equation:

$$\begin{align}
(a \otimes a) \oplus (2 \otimes a \otimes b) \oplus (b \otimes b) = c \otimes c
\end{align}$$

For example, $(a, b, c) = (1, 2, 1)$ is a solution to this equation, and so is $(1, 8, 13)$.

Let $F(N)$ be the number of solutions to this equation satisfying $0 \le a \le b \le N$. You are given $F(10)=21$.

Find $F(10^7)$.

Answer: 83357132


Given the representation of a continued fraction
$$ a_0+ \cfrac 1{a_1+ \cfrac 1{a_2+\cfrac 1{a_3+\ddots }}}= [a_0;a_1,a_2,a_3,\ldots] $$

$\alpha$ is a real number with continued fraction representation:
$\alpha = [2;1,1,2,1,1,1,2,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,2,\ldots]$
where the number of $1$'s between each of the $2$'s are consecutive prime numbers.

$\beta$ is another real number defined as
$$	\beta = \frac{2\alpha+3}{3\alpha+2} $$

The first ten coefficients of the continued fraction of $\beta$ are $[0;1,5,6,16,9,1,10,16,11]$ with sum $75$.

Find the sum of the first $10^8$ coefficients of the continued fraction of $\beta$.

Answer: 585787007


The $(a,b,m)$-sequence, where $0 \leq a,b \lt m$, is defined as

$\begin{align*}
g(0)&=a\\
g(1)&=b\\
g(n)&= \big(g(n-1) + g(n-2)\big) \bmod m
\end{align*}$

All $(a,b,m)$-sequences are periodic with period denoted by $p(a,b,m)$.

The first few terms of the $(0,1,8)$-sequence are $(0,1,1,2,3,5,0,5,5,2,7,1,0,1,1,2,\ldots )$ and so $p(0,1,8)=12$.

Let $\displaystyle s(m)=\sum_{a=0}^{m-1}\sum_{b=0}^{m-1} p(a,b,m)^2$. For example, $s(3)=513$ and $s(10)=225820$.

Define $\displaystyle S(M)=\sum_{m=1}^{M}s(m)$. You are given, $S(3)=542$ and $S(10)=310897$.

Find $S(10^6)$. Give your answer modulo $999\,999\,893$.

Answer: 213731313


Left and Right play a game with a word consisting of L's and R's, alternating turns. On Left's turn, Left can remove any positive number of letters, but not all the letters, from the left side of the word. Right does the same on Right's turn except that Right removes letters from the right side. The game continues until only one letter remains: if it is an 'L' then Left wins; if it is an 'R' then Right wins.

Let $F(n)$ be the number of words of length $n$ where the player moving first, whether it's Left or Right, will win the game if both play optimally.

You are given $F(3)=4$ and $F(8)=181$.

Find $F(60)$.

Answer: 1033654680825334184


Left and Right play a game with a number of words, each consisting of L's and R's, alternating turns. On Left's turn, for each word, Left can remove any number of letters (possibly zero), but not all the letters, from the left side of the word. However, at least one letter must be removed from at least one word. Right does the same on Right's turn except that Right removes letters from the right side of each word. The game continues until each word is reduced to a single letter. If there are more L's than R's remaining then Left wins; otherwise if there are more R's than L's then Right wins. In this problem we only consider games with an odd number of words, thus making ties impossible.

Let $G(n, k)$ be the number of ways of choosing $k$ words of length $n$, for which Right has a winning strategy when Left plays first. Different orderings of the same set of words are to be counted separately.

It can be seen that $G(2, 3)=14$ due to the following solutions (and their reorderings):
$$\begin{align}
(\texttt{LL},\texttt{RR},\texttt{RR})&:3\text{ orderings}\\
(\texttt{LR},\texttt{LR},\texttt{LR})&:1\text{ ordering}\\
(\texttt{LR},\texttt{LR},\texttt{RR})&:3\text{ orderings}\\
(\texttt{LR},\texttt{RR},\texttt{RR})&:3\text{ orderings}\\
(\texttt{RL},\texttt{RR},\texttt{RR})&:3\text{ orderings}\\
(\texttt{RR},\texttt{RR},\texttt{RR})&:1\text{ ordering}
\end{align}
$$You are also given $G(4, 3)=496$ and $G(8, 5)=26359197010$.

Find $G(20, 7)$ giving your answer modulo $1001001011$.

Answer: 726010935


A band of pirates has come into a hoard of treasure, and must decide how to distribute it amongst themselves. The treasure consists of identical, indivisible gold coins.

According to pirate law, the distribution of treasure must proceed as follows:

- The most senior pirate proposes a distribution of the coins.

- All pirates, including the most senior, vote on whether to accept the distribution.

- If at least half of the pirates vote to accept, the distribution stands.

- Otherwise, the most senior pirate must walk the plank, and the process resumes from step 1 with the next most senior pirate proposing another distribution.

The happiness of a pirate is equal to $-\infty$ if he doesn't survive; otherwise, it is equal to $c + p\cdot w$, where $c$ is the number of coins that pirate receives in the distribution, $w$ is the total number of pirates who were made to walk the plank, and $p$ is the bloodthirstiness of the pirate.

The pirates have a number of characteristics:

- Greed: to maximise their happiness.

- Ruthlessness: incapable of cooperation, making promises or maintaining any kind of reputation.

- Shrewdness: perfectly rational and logical.

Consider the happiness $c(n,C,p) + p\cdot w(n,C,p)$ of the most senior surviving pirate in the situation where $n$ pirates, all with equal bloodthirstiness $p$, have found $C$ coins. For example, $c(5,5,\frac{1}{10}) = 3$ and $w(5,5,\frac{1}{10})=0$ because it can be shown that if the most senior pirate proposes a distribution of $3,0,1,0,1$ coins to the pirates (in decreasing order of seniority), the three pirates receiving coins will all vote to accept. On the other hand, $c(5,1,\frac{1}{10}) = 0$ and $w(5,1,\frac{1}{10}) = 1$: the most senior pirate cannot survive with any proposal, and then the second most senior pirate must give the only coin to another pirate in order to survive.

Define $\displaystyle T(N,C,p) = \sum_{n=1}^N \left ( c(n,C,p) + w(n,C,p) \right )$. You are given that $T(30,3,\frac{1}{\sqrt{3}}) = 190$, $T(50,3,\frac{1}{\sqrt{31}}) = 385$, and $T(10^3, 101, \frac{1}{\sqrt{101}}) = 142427$.

Find $\displaystyle \sum_{k=1}^6 T(10^{16},10^k+1,\tfrac{1}{\sqrt{10^k+1}})$.  Give the last 9 digits as your answer.

Answer: 429162542


Two players play a game using a deck of $2n$ cards: $n$ red and $n$ black. Initially the deck is shuffled into one of the $\binom{2n}{n}$ possible starting configurations. Play then proceeds, alternating turns, where a player follows two steps on each turn:

-
Remove the top card from the deck, taking note of its colour.

-
If there is a next card and it is the same colour as the previous card they toss a fair coin. If the coin lands on heads they remove that card as well; otherwise leave it on top of the deck.

The player who removes the final card from the deck wins the game.

Some starting configurations give an advantage to one of the players; while some starting configurations are fair, in which both players have exactly $50\%$ chance to win the game. For example, if $n=2$ there are four starting configurations which are fair: RRBB, BBRR, RBBR, BRRB. The remaining two, RBRB and BRBR, result in a guaranteed win for the second player.

Define $F(n)$ to be the number of starting configurations which are fair. Therefore $F(2)=4$. You are also given $F(8)=11892$.

Find $F(26)$.

Answer: 495568995495726


Given a prime $p$ and a positive integer $n \lt p$, let $R(p, n)$ be the multiplicative order of $p$ modulo $n!$.

In other words, $R(p, n)$ is the minimal positive integer $r$ such that

$$p^r \equiv 1 \pmod{n!}$$

For example, $R(7, 4) = 2$ and $R(10^9 + 7, 12) = 17280$.

Find $R(10^9 + 7, 10^7)$. Give your answer modulo $10^9 + 7$.

Answer: 794394453


In the classical game of Nim two players take turns removing stones from piles. A player may remove any positive number of stones from a single pile. If there are no remaining stones, the next player to move loses.

In Factorisation Nim the initial position of the game is chosen according to the prime factorisation of a given natural number $n$ by setting a pile for each prime factor, including multiplicity. For example, if $n=12=2 \times 2 \times 3$ the game starts with three piles: two piles with two stones and one pile with three stones.

It can be verified that the first player to move loses for $n=1$ and for $n=70$, assuming both players play optimally.

Let $S(N)$ be the sum of $n$ for $1 \le n \le N$ such that the first player to move loses, assuming both players play optimally. You are given $S(10) = 14$ and $S(100) = 455$.

Find $S(10^{14})$. Give your answer modulo $10^9 + 7$.

Answer: 176907658


A positive integer is called heptaphobic if it is not divisible by seven and no number divisible by seven can be produced by swapping two of its digits. Note that leading zeros are not allowed before or after the swap.

For example, $17$ and $1305$ are heptaphobic, but $14$ and $132$ are not because $14$ and $231$ are divisible by seven.

Let $C(N)$ count heptaphobic numbers smaller than $N$. You are given $C(100) = 74$ and $C(10^4) = 3737$.

Find $C(10^{13})$.

Answer: 736463823


A sequence $(a_n)_{n \ge 0}$ starts with $a_0 = 3$ and for each $n \ge 0$,

- if $a_n$ is a triangle numberA triangle number is a number of the form $m(m + 1)/2$ for some integer $m$., then $a_{n + 1} = a_n + 1$;

- otherwise, $a_{n + 1} = 2a_n - a_{n - 1} + 1$.

The sequence begins:
$${\color{red}3}, 4, {\color{red}6}, 7, 9, 12, 16, {\color{red}21}, 22, 24, 27, 31, {\color{red}36}, 37, 39, 42, \dots$$
where triangle numbers are marked red.

The $10$th triangle number in the sequence is $a_{2964} = 1439056$.

Find the index $n$ such that $a_n$ is the $70$th triangle number in the sequence.

Answer: 6795261671274


The total number of prime factors of $n$, counted with multiplicity, is denoted $\Omega(n)$.

For example, $\Omega(12)=3$, counting the factor $2$ twice, and the factor $3$ once.

Define $D(n, m)$ to be the sum of all divisors $d$ of $n$ where $\Omega(d)$ is divisible by $m$.

For example, $D(24, 3)=1+8+12=21$.

The superfactorial of $n$, often written as $n\$$, is defined as the product of the first $n$ factorials:
$$n\$=1!\times 2! \times\cdots\times n!$$
The superduperfactorial of $n$, we write as $n\bigstar$, is defined as the product of the first $n$ superfactorials:
$$n\bigstar=1\$ \times 2\$ \times\cdots\times n\$ $$

You are given $D(6\bigstar, 6)=6368195719791280$.

Find $D(1\,000\bigstar, 1\,000)$.
Give your answer modulo $999\,999\,001$.

Answer: 882086212


There is a plane on which all points are initially white, except three red points and two blue points.

On each day, every line passing through a red point and a blue point is constructed. Then every white point, where two different such lines meet, turns blue.

Let $g(n)$ be the maximal possible number of blue points after $n$ days.

For example, $g(1)=8$ and $g(2)=28$.

Find $g(16)$.

Answer: 234897386493229284


The Euclidean algorithm can be used to find the greatest common divisor of two positive integers. At each step of the algorithm the smaller number is subtracted from the larger one. The algorithm terminates when the numbers are equal, which is then the greatest common divisor of the original numbers.

For two numbers $n$ and $m$, let $d(n, m)$ be the number of subtraction steps used by the Euclidean algorithm for computing the greatest common divisor of $n$ and $m$.

For a number $n$, let $f(n)$ be the positive number $m$ coprime to $n$ that minimizes $d(n, m)$. If more than one number attains the minimum, the minimal $m$ is chosen.

For example, at least four steps are needed for computing the GCD of $7$ and any positive number $m$ coprime to $7$. This number of steps is obtained by $m=2,3,4,5$, yielding $f(7)=2$. You are also given $f(89)=34$ and $f(8191) = 1856$.

Find $f(10^{12}+39)$.

Answer: 367554579311


A frog is placed on the number line. Every step the frog jumps either $a$ units to the left or $b$ units to the right, both with $1/2$ probability.

Define $f(a, b)$ as the limit $\lim_{n \to \infty} \frac{c_n}n$ where $c_n$ is the expected number of unique numbers visited in the first $n$ steps. You are given $f(1, 1) = 0$ and $f(1, 2) \approx 0.427050983$.

Find $f(89, 97)$. Give your answer rounded to nine digits after the decimal point.

Answer: 0.857162085


There are $n$ distinct piles of stones, each of size $n-1$. Starting with an initial score of $0$, the following procedure is repeated:

- Choose any two piles and remove exactly $n$ stones in total from the two piles.

- If the number of stones removed from the two piles were $a$ and $b$, add $\min(a,b)$ to the score.

If all piles are eventually emptied, the current score is confirmed as final. However, if one gets "stuck" and cannot empty all piles, the current score is discarded, resulting in a final score of $0$.

Three example sequences of turns are illustrated below for $n=4$, with each tuple representing pile sizes as one proceeds, and with additions to the score indicated above the arrows.
$$
\begin{align}
&(3,3,3,3)\xrightarrow{+1}(0,3,2,3)\xrightarrow{+1}(0,3,1,0)\xrightarrow{+1}(0,0,0,0)&:\quad\text{final score }=3\\
&(3,3,3,3)\xrightarrow{+1}(3,0,3,2)\xrightarrow{+2}(1,0,3,0)\xrightarrow{+1}(0,0,0,0)&:\quad\text{final score }=4\\
&(3,3,3,3)\xrightarrow{+2}(1,3,1,3)\xrightarrow{+1}(1,2,1,0)\rightarrow\text{stuck!}&:\quad\text{final score }=0
\end{align}
$$

Define $F(n)$ to be the sum of the final scores achieved for every sequence of turns which successfully empty all piles.

You are given $F(3)=12$, $F(4)=360$, and $F(8)=16785941760$.

Find $F(100)$. Give your answer modulo $10^9+7$.

Answer: 243559751


This game starts with a positive integer. Two players take turns to remove a single digit from that integer. After the digit is removed any resulting leading zeros are removed.

For example, removing a digit from $105$ results in either $5$, $10$ or $15$.

The winner is the person who removes the last nonzero digit.

Define $W(N)$ to be how many positive integers less than $N$ for which the first player can guarantee a win given optimal play. You are given $W(100) = 18$ and $W(10^4) = 1656$.

Find $W(10^{18})$.

Answer: 166666666689036288


Given is an integer sided triangle $ABC$ with $BC \le AC \le AB$.
$k$ is the angular bisector of angle $ACB$.
$m$ is the tangent at $C$ to the circumscribed circle of $ABC$.
$n$ is a line parallel to $m$ through $B$.

The intersection of $n$ and $k$ is called $E$.

How many triangles $ABC$ with a perimeter not exceeding $1\,000\,000$ exist such that $CE$ has integral length?

Note: This problem is a more difficult version of Problem 296. Please pay close attention to the differences between the two statements.

Answer: 7259046


NOTE: This problem is related to Problem 882. It is recommended to solve that problem before doing this one.

Two players are playing a game. When the game starts, each player holds a paper with two positive integers written on it.

They make moves in turn. At a player's turn, the player can do one of the following:

- pick a number on the player's own paper and change it by removing a $0$ from its ternary expansionbase-$3$ expansion;

- pick a number on the opponent's paper and change it by removing a $1$ from its ternary expansion;

- pick a number on either paper and change it by removing a $2$ from its ternary expansion.

The player that is unable to make a move loses.

Leading zeros are not allowed in any ternary expansion; in particular nobody can make a move on the number $0$.

An initial setting is called fair if whichever player moves first will lose the game if both play optimally.

For example, if initially the integers on the paper of the first player are $1, 5$ and those on the paper of the second player are $2, 4$, then this is a fair initial setting, which we can denote as $(1, 5 \mid 2, 4)$.

Note that the order of the two integers on a paper does not matter, but the order of the two papers matter.

Thus $(5, 1 \mid 4, 2)$ is considered the same as $(1, 5 \mid 2, 4)$, while $(2, 4 \mid 1, 5)$ is a different initial setting.

Let $F(N)$ be the number of fair initial settings where each initial number does not exceed $N$.

For example, $F(5) = 21$.

Find $F(10^5)$.

Answer: 55129975871328418


A group of $k(k-1) / 2 + 1$ children play a game of $k$ rounds.

At the beginning, they are all seated on chairs arranged in a circle.

During the $i$-th round:

- The music starts playing and $i$ children are randomly selected, with all combinations being equally likely.  The selected children stand up and dance around.

- When the music stops, these $i$ children sit back down randomly in the $i$ available chairs, with all permutations being equally likely.

Let $P(k)$ be the probability that every child ends up sitting exactly one chair to the right of their original chair when the game ends (at the end of the $k$-th round).

You are given $P(3) \approx 1.3888888889 \mathrm {e}{-2}$.

Find $P(7)$. Give your answer in scientific notation rounded to ten significant digits after the decimal point. Use a lowercase e to separate the mantissa and the exponent.

Answer: 4.7126135532e-29


Let $\{x\}$ denote the fractional part of a real number $x$.

Define $f_N(x)$ to be the minimal value of $\{nx\}$ for integer $n$ satisfying $0 < n \le N$.

Further define $F(N)$ to be the expected value of $f_N(x)$ when $x$ is sampled uniformly in $[0, 1]$.

You are given $F(1) = \frac{1}{2}$, $F(4) = \frac{1}{4}$ and $F(10) \approx 0.1319444444444$.

Find $F(10^4)$ and give your answer rounded to 13 digits after the decimal point.

Answer: 0.0003452201133


Let $I(a, b, c)$ be the largest possible area of intersection between a triangle of side lengths $a, b, c$ and a circle which has the same area as the triangle.

For example $I(3, 4, 5) \approx 4.593049$ and $I(3, 4, 6) \approx 3.552564$.

Find the sum of $I(a, b, c)$ for integers $a, b, c$ such that $1 \le a \le b \le c \lt a + b$ and $a + b + c \le 200$.

Give your answer rounded to two digits after the decimal point.

Answer: 29337152.09


A positive integer $n$ is considered $B$-trivisible if the sum of all different prime factors of $n$ which are not larger than $B$ is divisible by $3$.

For example, $175 = 5^2 \cdot 7$ is $10$-trivisible because $5 + 7 = 12$ which is divisible by $3$. Similarly, $175$ is $4$-trivisible because all primes dividing $175$ are larger than $4$, and the empty summation $0$ is divisible by $3$.

On the other hand, $175$ is not $6$-trivisible because the sum of relevant primes is $5$ which is not divisible by $3$.

Let $F(N, B)$ be the number of $B$-trivisible integers not larger than $N$.

For example, $F(10, 4) = 5$, the $4$-trivisible numbers being $1,3,5,7,9$.

You are also given $F(10, 10) = 3$ and $F(100, 10) = 41$.

Find $F(10^{18}, 120)$.

Answer: 357591131712034236


Define
$$P(X_{a,b},X_{a,c},X_{a,d},X_{a,e},X_{b,c},X_{b,d},X_{b,e},X_{c,d},X_{c,e},X_{d,e})$$
as the sum of $2^a3^b5^c7^d11^e$ over all quintuples of non-negative integers $(a, b, c, d, e)$ such that the sum of each two of the five variables is restricted by a given value. In other words, $a+b \le X_{a,b}$, $a+d \le X_{a,d}$, $b+e \le X_{b,e}$ etc.

For example, $P(2,2,2,2,2,2,2,2,2,2)=7120$ and $P(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) \equiv 799809376 \pmod{10^9 + 7}$.

Define a sequence $A$ as follows:

- $A_0 = 1$, $A_1 = 7$;

- $A_n =(7A_{n−1}+A_{n-2}^2) \bmod(10^9+7)$ for $n \ge 2$.

Also define $Q(n) = P(A_{10n}, A_{10n+1}, A_{10n+2}, \dots , A_{10n+9})$.

Find $\displaystyle\sum_{0 \le n \lt 100}Q(n)$. Give your answer modulo $10^9+7$.

Answer: 885362394


Starting at zero, a kangaroo hops along the real number line in the positive direction. Each successive hop takes the kangaroo forward a uniformly random distance between $0$ and $1$. Let $H(n)$ be the expected number of hops needed for the kangaroo to pass $n$ on the real line.

If we write $\alpha = H(1)$, then for all positive integers $n$, $H(n)$ can be expressed as a polynomial function of $\alpha$ with rational coefficients. For example $H(3)=\alpha^3-2\alpha^2+\frac{1}{2}\alpha$. Define $S(n)$ to be the sum of all integer coefficients in this polynomial form of $H(n)$. Therefore $S(1)=1$ and $S(3)=1+(-2)=-1$.

You are also given $\displaystyle \sum_{n=1}^{10} S(n)=43$.

Find $\displaystyle\sum_{n=1}^{10^{18}} S(n)$. Give your answer modulo $10^9+7$.

Answer: 412543690


Starting at zero, a kangaroo hops along the real number line in the positive direction. Each successive hop takes the kangaroo forward a uniformly random distance between $0$ and $1$. Let $H(n)$ be the expected number of hops needed for the kangaroo to pass $n$ on the real line.

For example, $H(2) \approx 4.67077427047$. The first eight digits after the decimal point that are different from six are $70774270$.

Similarly, $H(3) \approx 6.6665656395558899$. Here the first eight digits after the decimal point that are different from six are $55395558$.

Find $H(10^6)$ and give as your answer the first eight digits after the decimal point that are different from six.

Answer: 44754029


Let $p$ be a prime of the form $5k-4$ and define $f_p(x) = \left(x^k+x\right) \bmod p$.

Let $C(p)$ be the number of values $0 \le x \lt p$ such that $f_p^{(m)}(x) = x$ for some positive integer $m$, that is, $x$ can be obtained by iteratively applying $f_p$ on itself starting at $x$.

For example, $C(11) = 7$, due to $x = 0, 1, 2, 3, 8, 9, 10$.

Let $S(N)$ be the sum of $C(p)$ for all primes of the form $5k-4$ not exceeding $N$. For example $S(100) = 127$.

Find $S(10^8)$.

Answer: 33626723890930


The hyperbolic plane can be represented by the open unit disc, namely the set of points $(x, y)$ in $\Bbb R^2$ with $x^2 + y^2 < 1$.

A geodesic is defined as either a diameter of the open unit disc or a circular arc contained within the disc that is orthogonal to the boundary of the disc.

The following diagram shows the hyperbolic plane with two geodesics; one is a diameter and the other is a circular arc.

Let $\mathcal V(N)$ be the set of points $(x, y)$ such that $x^2 + y^2 \lt 1$ and $x, y$ are both rational numbers with denominator not exceeding $N$.

Let $T(N)$ be the number of ordered triples $(P, Q, R)$ such that $P, Q, R$ are three different points in $\mathcal V(N)$ and there is a hyperbolic line passing through all of them.

For example, $T(2) = 24$ and $T(3) = 1296$.

Find $T(12)$.

Answer: 3575508


A game is played with $n$ cards.
At the start the cards are dealt out onto a table to get $n$ piles of size one.

Each round proceeds as follows:

- Select a pile at random and pick it up.

- Randomly choose a pile from the table and add the top card of the picked-up pile to it.

- Redistribute any remaining cards from the picked-up pile by dealing them into new single-card piles.

The game ends when all cards are in a single pile.

At the end of each round a score is obtained by bitwise-XORing the size of each pile. The score is summed across the rounds. Let $X(n)$ be the expected total score at the end of the game.

You are given $X(2) = 2$, $X(4) = 14$ and $X(10) = 1418$.

Find $X(10^4)$. Give your answer modulo $10^9+7$.

Answer: 427278142


A very odd number is a number which contains only odd digits and is divisible by $105$.
Further each odd digit occurs an odd number of times.

Define $\Theta (n)$ be the $n$th very odd number, then $\Theta (1) = 1117935$ and $\Theta(10^3) = 11137955115$.

Find $\Theta(10^{16})$.


Given a pair $(a,b)$ of coprime odd positive integers, define the function
$$H_{a,b}(x)=\frac{1}{2}-\frac{1}{2(a+b)}\Bigl(b\cos(a\pi x)+a\cos(b\pi x)\Bigr)
$$It can be seen that $H_{a,b}(0)=0$, $H_{a,b}(1)=1$, and $0 < H_{a,b}(x) < 1$ for all $x$ strictly between $0$ and $1$.

Given two such pairs $(a,b)$ and $(c,d)$, paths of infinitesimal width traverse the unit cube internally through every point $(x,y,z)\in [0,1]^3$ such that $z=H_{a,b}(x)=H_{c,d}(y)$. Remarkably, it can be shown that the point $(0,0,0)$ is always connected to the opposite corner $(1,1,1)$. Furthermore, with the additional condition $\gcd(a+b,c+d)\in\{2,4\}$, it can be shown that there is exactly one path connecting the two points.

Shown above are two examples, as viewed from above the cube. That is, we see the paths projected onto the $xy$-plane, with corresponding $z$ values indicated with varying colour. In the second example some paths are coloured grey to indicate that, while they exist, they do not form part of the path from $(0,0,0)$ to $(1,1,1)$.

Define $F(a, b, c, d)$ to be the sum of the absolute changes in height (or $z$-coordinate) over all uphill and downhill sections of the path from $(0,0,0)$ to $(1,1,1)$. In the first example above, the path climbs $\approx4.00886$ over eleven uphill sections, and descends $\approx3.00886$ over ten downhill sections, giving $F(3,5,3,7)\approx7.01772$. You are also given $F(7,17,9,19)\approx 26.79578$.

Let $G(m, n)$ be the sum of $F(p,q,p,2q-p)$ over all pairs $(p,q)$ of primes, $m\leq p < q\leq n$. You are given $G(3, 20)\approx463.80866$.

Find $G(500,1000)$ giving your answer rounded to five digits after the decimal point.

Answer: 88597366.47748


Two players X and O play a game with $k$ strips of squares of lengths $n_1,\dots,n_k$, originally all blank.

Starting with X, they make moves in turn. At X's turn, X draws an "X" symbol; at O's turn, O draws an "O" symbol.

The symbol must be drawn in one blank square with either red or blue pen, subject to the following restrictions:

- two symbols in adjacent squares on one strip must be different symbols and must have different colour;

- if there is at least one blank strip, then one must draw on a blank strip.

Whoever does not have a valid move loses the game.

Let $P(K, N)$ be the number of tuples $(n_1,\dots,n_k)$ such that $1 \leq k \leq K$, $1\leq n_1\leq\cdots\leq n_k\leq N$ and that X has a winning strategy to the corresponding game.

For example, $P(2, 4)=7$ and $P(5, 10) = 901$.

Find $P(10^7, 10^7)\bmod 1234567891$.

Answer: 675608326


For a positive integer $n$, let $F(n)$ denote the number of functions $f$ from the set $S_n=\{1,2,\dots,n\}$ to itself such that $f^{(x)}(y)=f^{(y)}(x)$ for any $x,y$ in $S_n$. Here $f^{(k)}$ denotes the $k$-th iterated composition of $f$, e.g. $f^{(2)}(x)=f(f(x))$.

For example, $F(3)=8$, $F(7)=174$, $F(100)=570271270297640131$.

Find $F(10^6) \bmod (10^9+7)$.

Answer: 537945304


In this problem we consider a random walk on the integers $\mathbb{Z}$, in which our position at time $t$ is denoted as $X_t$.

At time $0$ we start at position $0$. That is, $X_0=0$.

At time $1$ we jump to position $1$. That is, $X_1=1$.

Thereafter, at time $t=2,3,\dots$ we make a jump of size $|X_{t-2}|$ in either the positive or negative direction, with probability $1/2$ each way. If $X_{t-2}=0$ we stay put at time $t$.

At $t=5$ we find our position $X_5$ has the following distribution:
$$
X_5=\begin{cases}
-1\quad&\text{with probability }3/8\\
1\quad&\text{with probability }3/8\\
3\quad&\text{with probability }1/8\\
5\quad&\text{with probability }1/8\\
\end{cases}
$$
The standard deviation $\sigma$ of a random variable $X$ with mean $\mu$ is defined as
$$
\sigma=\sqrt{\mathbb{E}[X^2]-\mu^2}
$$
Furthermore the skewness of $X$ is defined as
$$
\text{Skew}(X)=\mathbb{E}\biggl[\Bigl(\frac{X-\mu}{\sigma}\Bigr)^3\biggr]
$$
For $X_5$, which has mean $1$ and standard deviation $2$, we find $\text{Skew}(X_5)=0.75$. You are also given $\text{Skew}(X_{10})\approx2.50997097$.

Find $\text{Skew}(X_{50})$. Give your answer rounded to eight digits after the decimal point.

Answer: 254.54470757


The hyperbolic plane, represented by the open unit disc, can be tiled by heptagons. Every tile is a hyperbolic heptagon (i.e. it has seven edges which are segments of geodesics in the hyperbolic plane) and every vertex is shared by three tiles.

Please refer to Problem 972 for some of the definitions.

The diagram below shows an illustration of this tiling.

Now, a hyperbolic frog starts from one of the heptagons, as shown in the diagram. At each step, it can jump to any one of the seven adjacent tiles.

Define $F(n)$ to be the number of paths the frog can trace so that after $n$ steps it lands back at the starting tile.

You are given $F(4) = 119$.

Find $F(20)$.

Answer: 189306828278449


Starting from an empty string, we want to build a string with letters "x", "y", "z". At each step, one of the following operations is performed:

- insert two consecutive identical letters "xx", "yy" or "zz" anywhere into the string;

- replace one letter in the string with two consecutive letters, according to the rule: "x" $\to$ "yz", "y" $\to$ "zx", "z" $\to$ "xy";

- exchange two consecutive different letters in the string, e.g. "xy" $\to$ "yx", "zx" $\to$ "xz", etc.

A string is called neutral if it is possible to produce the string from the empty string after an even number of steps.

We define a sequence $(a_n)_{n \ge 0}$: $a_0=88\,888\,888$ and $a_n=(8888\cdot a_{n-1})\bmod 888\,888\,883$ for $n \gt 0$.

Let $b_n = a_n \bmod 3$. For each $i \ge 0$, a string $c(i)$ of length $50$ is defined by translating the finite sequence $b_{50i},b_{50i+1},\dots,b_{50i+49}$ via the rule: $0 \to$ "x", $1 \to$ "y", $2 \to$ "z".

Let $F(N)$ be the number of ordered pairs $(i, j)$ with $0 \le i, j \lt N$ such that the concatenated string $c(i)c(j)$ is neutral.

For example, $F(10) = 13$ and $F(100) = 1224$.

Find $F(10^6)$.

Answer: 124999683766


Starting from an empty string, we want to build a string with letters "x", "y", "z". At each step, one of the following operations is performed:

- insert two consecutive identical letters "xx", "yy" or "zz" anywhere into the string;

- replace one letter in the string with two consecutive letters, according to the rule: "x" $\to$ "yz", "y" $\to$ "zx", "z" $\to$ "xy";

- exchange two consecutive different letters in the string, e.g. "xy" $\to$ "yx", "zx" $\to$ "xz", etc.

A string is called neutral if it is possible to produce the string from the empty string after an even number of steps.

Let $N(X, Y, Z)$ be the number of neutral strings which contain $X$ copies of "x", $Y$ copies of "y" and $Z$ copies of "z".

For example, $N(2, 2, 2) = 42$ and $N(8, 8, 8) = 4732773210$.

Find the sum of $N(i^3, j^3, k^3)$ for $0 \le i, j, k \lt 88$. Give your answer modulo $888\,888\,883$.

Answer: 794963735