
Problems of Google Code Jam Qualification Round 2013 can be found at http://code.google.com/codejam/contest/2270488/dashboard
1 Problem A. Tic-Tac-Toe-Tomek
No math involved, but complex number is builtin in python and go, so I use 0,i,1,−1
to represent “ “, T, O, X. So if‖∑4i=1ai,i‖>3
, then some one won.
2 Problem B. Lawnmower
Theorem.(ai,j)
is a possible pattern if
ai,j=min(max({ai,k}),max({ak,j}))
for every i,j
.
Proof.
Can be easily proved by induction on N=∑i,jM−ai,j
, where M=max({ai,j})
.
3 Problem C. Fair and Square
Let
N=an10n+an−110n−1+⋯+a110+a0
, where ai∈{0,1,…9}
and an≠0
. Denote by δk(x)
the (n−k+1)
-th digit of x
, then we have δk(x)=ak
.
Assume N
is a palindrome, i.e., ai=an−1
for every i
.
Let bk
be the coefficient of xk
of
(anxn+an−1xn−1+⋯+a1+a0)2,
that is,
bk=min(k,n)∑i=max(0,k−n)aiak−i.
Theorem. N2
is also a palindrome iff bk≤9
for all k
.
Proof.
(⇐
)
Trivial.
(⇒
)
Observe that bk=b2n−k
for all k
. We prove the statement by mathematical induction on k
for k≤n
.
Case k=0
:
Since
a2n102n≤N2<(an+1)2102n
and
δ0(N2)=δ0(a20)=δ0(a2n),
it is easy to see that a0=an∈{1,2,3}
. So b0=a20≤9
.
Moreover, we also know that N2
has 2n+1
digits and hence δ2n−k(N2)=δk(N2)
for all k≤2n
.
Case k>0
:
Suppose bi≤9
for all i<k
, we want to show bk≤9
.
By the induction hypothesis, we know that δ2n−i(N2)=δi(N2)=bi=b2n−i
for all i<k
. So
102n−k−1⋅⌊N2102n−k−1⌋=2n∑i=2n−k−1δi(N2)10i=2n∑i=2n−k−1bi10i
On the other hand,
⌊N2102n−k−1⌋=⌊∑2ni=0bi10i102n−k−1⌋=∑2ni=2n−k−1bi10i102n−k−1+⌊∑2n−ki=0bi10i102n−k−1⌋.
Therefore, ⌊∑2n−ki=0bi10i102n−k−1⌋=0
, hence
bk=b2n−k≤∑2n−ki=0bi10i102n−k<10.
Proof.
bn=∑ni=0aian−i=∑ni=0a2i
.
(⇐
)
bn≥bk
for every k
by Cauchy-Schwarz inequility.
(⇒
)
Trivial.
In particular, if N≠3
, then ai∈{0,1,2}
.
4 Problem D. Treasure
Let T⊆N
be the set of all key types. Assume 0∉T
.
Construct a directed graph G=(V,E)
as following:
- V=T∪{0}
- (0,j)∈E iff we starts with at least one key of type j .
- If i≠0 , then (i,j)∈E iff there is a key of type j in a box of type i .bn≥bk for every palidrone N by Cauchy-Schwarz inequility.
Denote by B(i)
the number of boxes of type i
and K(i)
the total number of keys of type i
(including the keys you start with and keys in boxes).
Theorem. All box can be opened iff for every j∈T
, B(j)≤K(j)
and there is a directed path from 0
to j
in graph G
.
Proof.
(⇒
)
If B(j)>K(j)
, then we don’t have enough keys of type j
to open all boxes of type j
.
If there is no path from 0
to j
, then it means we are unable to get any key of type j
.
(⇐
)
We prove this by induction on the number of boxes. Denote by N
the number of boxes.
Assume for every j∈T
, B(j)≤K(j)
and there is a directed path from 0
to j
in graph G
.
Case N=1
:
Then all vertices in G
are leaves except 0
and the type of the only box. So there must be an edge from 0 to the type of the box.
Case N>1
:
Suppose the theorem holds for the case with N−1
boxes.
There are at least one (0,i)∈E
. Consider we open a box of type i
(that contains a key of type i
if possible). Assume we destroy the key and the box just opened, then we have a setting of N−1
boxes. In this setting, we still have B(j)≤K(j)
for all j
. Let G′=(V′,E′)
be the directed graph of this (N−1)
-box setting.
If B(i)=1
, then all children of j
in G
become children of 0
, 0
can still reach all other types in G′
.
Assume B(i)>1
.
If (i,i)∈E
, then we can open a box of type i
that contains a key of type i
.
Otherwise, because K(i)≥B(i)>1
, we still hold at least one key of type i
after we open a box of type i
(and destroy the key).
Either way, (0,i)∈E′
. Therefore, for every child j
of i
in G
, either
1. (0,j)∈E′
(if a key of type j
is in the box we just opened), or
2. (i,j)∈E′
and so there is a path from 0
to i
and then to j
.
So 0
can still reach very vertex in G′
.
By induction hypothesis, the remaining N−1
can also be opened.
By the proof of the theorem, there is a simple algorithm to open all boxes: open any box unless you have just one key of that type and box does not contain a key that can open itself.
However, this algorithm may not yield a "lexicographically smallest" way to open the boxes.
The solve the problem, you should open the box with smallest number such that either this box is the last one of type i
or there is still a way to get a type i
key.
No comments:
Post a Comment