Aperiodic Binary Strings Redux

A binary string is a string whose characters are only 0 s and/or 1 s. For example, 0101 , 11111 , 110001 0101, 11111, 110001 are binary strings.

A string s s is periodic if there exists another string t t whose length is less than s s such that concatenating several copies of t t produces s s . For example, 0101 0101 is periodic ( t = 01 t = 01 ), and 1111 1111 is periodic ( t = 11 t = 11 or t = 1 t = 1 ), but 10101 10101 is not periodic. A string is aperiodic if it is not periodic.

This problem asks about the number of aperiodic binary strings of length 23 23 . For this problem, compute the number of aperiodic binary strings of length ( 23 ! ) 23 (23!)^{23} (that is, 23 factorial raised to the 23th power). Enter your answer modulo 1 0 9 + 7 = 1000000007 10^9 + 7 = 1000000007 .


Clearly inspired from the linked problem above .


The answer is 676103472.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Ivan Koswara
Nov 28, 2015

Let's skip to the formal stuff immediately. For a gentler introduction, try the linked problem above.

First, definitions.

  • If s s is a string, define s |s| to be its length.
  • If s s is a string, let t t be a string such that s s is made up of several (probably one) copies of t t ; call t t to be a building block of s s . (A string is its own building block; the periodic strings are exactly the strings that have another building block besides its own. There can be multiple building blocks; for example, 1111 1111 has building blocks 1111 , 11 , 1 1111, 11, 1 .)
  • If s s is a string and t t is (one of) its building block, let t t 's repeat count be s t \frac{|s|}{|t|} ; that is, the number of copies of t t necessary to make s s .
  • Define n n to be a natural number such that we want to compute the number of aperiodic binary strings of length n n . (That is, n = ( 23 ! ) 23 n = (23!)^{23} in this problem, but we generalize.)
  • Define P P to be the set of prime divisors of n n .
  • If X X is a subset of the natural numbers, define Π ( X ) \Pi(X) to be the product of its elements.
  • For all X P X \subset P , define S X S_X to be the set of all binary strings of length n n which has a building block with repeat count Π ( X ) \Pi(X) .

Now, the claim: the number of aperiodic strings of length n n is

X P ( 1 ) S 2 n Π ( X ) \displaystyle\Large{\sum_{X \subset P} (-1)^{|S|} \cdot 2^{\frac{n}{\Pi(X)}}}

To prove this is correct, we will begin with several simple observations about our S I S_I 's.

Result 1 : If s s is periodic with repeat count m m , then m m divides s |s| .
Trivial; having m m copies of a string means the total length is a multiple of m m .

Result 2 : If s s is periodic with repeat count m m and d m d|m , then s s is periodic with repeat count d d . If s s is made up of building block t t with repeat count m m , then s s is also made up of building block t t' with repeat count d d , where t t' is t t repeated m d \frac{m}{d} times. The corollary uses the fact that Π ( A ) Π ( B ) \Pi(A) | \Pi(B) .

Result 3 : S S_\emptyset is precisely the set of all binary strings of length n n .
Result 4 : If X X \neq \emptyset , S X S_X only contains periodic binary strings (of length n n ).
Trivial by definition.

Result 5 : Every periodic binary string (of length n n ) is contained in S { d } S_{\{d\}} for some d P d \in P . This follows from Result 2. Suppose s s is periodic with repeat count m m , and let d d be one prime divisor of m m (exists because m 1 m \neq 1 by definition of being periodic). Then s s is also periodic with repeat count d d . Since d d is a prime number, it is also necessarily a prime divisor of n n (Result 1), and this implies s S { d } s \in S_{\{d\}} .

Result 6 : If A , B P A,B \subset P , then S A S B = S A B S_A \cap S_B = S_{A \cup B} .
This should be easy, but strangely I can't find a proof for this. I'll edit this later.

Result 7 : If A , B , C A,B,C are sets such that B , C A B,C \subset A , then A ( B C ) = A C B ( B C ) |A \setminus (B \cup C)| = |A \setminus C| - |B \setminus (B \cap C)| .
Straightforward: the first term removes all in C C , the second term removes all in B B that aren't in C C and thus missed by the first term.

Result 8 : If X P X \subset P , then S X = 2 n / Π ( X ) |S_X| = 2^{n/\Pi(X)} .
Straightforward. All building blocks are of length n Π ( X ) \frac{n}{\Pi(X)} (because they are repeated Π ( X ) \Pi(X) times, and so we're counting binary strings of length n Π ( X ) \frac{n}{\Pi(X)} .

Let A A be our sought number. By Results 3, 4, 5, we have A = S d P S { d } A = \left| S_\emptyset \setminus \cup_{d \in P} S_{\{d\}} \right| . By Results 6 and 7, we can implement this effectively by recursion. (This is also the Principle of Inclusion-Exclusion.) The base case is just a single set (without anything subtracted from it), which Result 8 can handle. We can actually expand this recursion fully to show that it's equal to the claim above.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import itertools

MOD = 1000000007

# one-liner factorial
def factorial(n): return 1 if n == 0 else n * factorial(n-1)

def factorize(n):
    # factorizes number n into [(f1,e1), (f2,e2), ...]
    # where n = f1^e1 * f2^e2 * ...
    divisor = 2
    result = []
    while n > 1:
        count = 0
        while n % divisor == 0:
            n //= divisor
            count += 1
        if count: result.append((divisor, count))
        divisor += 1
    return res

def triple_pow(a, b, c):
    # computes a^(b^c)
    result = a
    for i in range(c): result = pow(result, b, MOD)
    return result

def solve(factors, value=2):
    # factors is [(f1,e1), (f2,e2), ...]
    # finds number of aperiodic binary strings on f1^e1 * f2^e2 * ... bits
    if len(factors) == 0: return value
    f = factors[0]
    return (solve(factors[1:], triple_pow(value, f[0], f[1])) -
            solve(factors[1:], triple_pow(value, f[0], f[1]-1))) % MOD

number = factorial(23) ** 23
factors = factorize(number)
print(solve(factors))
# prints 676103472

Handling ( 23 ! ) 23 (23!)^{23} is hard; we will denote our number in its prime factorization instead. Our number n n will be represented as [(f1,e1), (f2,e2), ...] , which means n = f 1 e 1 f 2 e 2 n = f_1^{e_1} f_2^{e_2} \ldots . The functions factorial and factorize , along with the assignment to factors at the end, are just means to compute the factorization. (There should be a more efficient method on doing this, but I suppose this is the clearest. If necessary, you can sidestep it entirely by computing the expansion by hand, also to avoid having problems with finite precision integers.)

triple_pow computes a b c a^{b^c} . Since our number is represented in its prime factorization, we will use this a lot. For example, computing 2 n 2^n becomes computing 2 f 1 e 1 f 2 e 2 = ( ( 2 f 1 e 1 ) f 2 e 2 ) 2^{f_1^{e_1} f_2^{e_2} \ldots} = ((2^{f_1^{e_1}})^{f_2^{e_2}})^\ldots . Of course, we apply modulo here, since we don't actually need to care about the result, just its value modulo 1 0 9 + 7 10^9+7 .

solve is the meat of the problem, which implements the recursion in Result 7 in a straightforward manner.


Another method to compute A A is possible in this manner, thanks to Challenge Master. Let A ( k ) A(k) be the number of aperiodic strings of length k k . For every binary string of length n n , there is a unique smallest building block, which is necessarily aperiodic; thus for every binary string of length n n , we can map it to an aperiodic string. All aperiodic strings considered have lengths that divide n n ; additionally, if d n d|n and t t is an aperiodic string of length d d , then it has a unique pre-image to some string of length n n (namely t t repeated n d \frac{n}{d} times). Thus we have the equality 2 n = d n A ( d ) 2^n = \sum_{d|n} A(d) : every binary string of length n n is mapped uniquely to some aperiodic string of length that divides n n , and all such aperiodic string has a pre-image. Using the Mobius inversion formula, we obtain A ( n ) = d n μ ( d ) 2 n / d A(n) = \sum_{d|n} \mu(d) 2^{n/d} . Since μ ( d ) = 0 \mu(d) = 0 if d d is not square-free, the only terms that matter have d d that is square-free, and thus we can map it to a subset X X of prime divisors of n n , where μ ( d ) = ( 1 ) X \mu(d) = (-1)^{|X|} and d = Π ( X ) d = \Pi(X) ; this gives the same formula as above.

Moderator note:

A slightly faster approach would be to find the Mobius inversion of 2 n 2^n . As pointed out, if we associate each binary string to it's smallest Aperiodic part, we get the bijection that d n A ( d ) = 2 n \sum_{d \mid n } A(d) = 2^n . Applying the Mobius inversion formula, we get that A ( n ) = d n μ ( d ) 2 n / d A(n) = \sum_{ d \mid n} \mu(d) 2^{ n / d} . This allows us to ignore divisors that have a square term, which means that we only need to look at 2 9 2^9 summands only.

Challenge Master: At the end, I do actually compute that number, although the method to reach it is a little roundabout with PIE. However, that method is very elegant, that I'll probably edit it into the solution. I guess I'm just not well-versed enough at number theory to think of that.

Ivan Koswara - 5 years, 6 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...