K-equivalence

Time limit1sMemory limit128 MB

Summary
Given a finite set K of positive integers described as disjoint intervals, determine which decimal digits 1-9 can always be swapped in any number of K without leaving K, and output the resulting equivalence classes.
Level

Hard8 of 10

Topics
Math, Simulation, Implementation
Solved
No attempts yet

Problem

Consider a set KK of positive integers.

Let pp and qq be two non-zero decimal digits. We call them KK-equivalent when the following holds:

For every n∈Kn \in K, replacing a single digit pp by qq, or a single digit qq by pp, in the decimal representation of nn always yields a number that is again an element of KK.

For example, if KK is the set of integers divisible by 33, then the digits 11, 44, and 77 are KK-equivalent: replacing a 11 by a 44 in the decimal representation of a number never changes its divisibility by 33.

KK-equivalence is an equivalence relation on the digits (it is reflexive, symmetric, and transitive).

You are given a finite set KK expressed as a union of pairwise-disjoint finite intervals of positive integers. Find the equivalence classes of the digits 11 through 99.

Input

The first line contains nn, the number of intervals whose union forms KK (1≤n≤100001 \le n \le 10000).

Each of the next nn lines contains two positive integers aia_i and bib_i that describe the interval [ai,bi][a_i, b_i] (all integers xx with ai≤x≤bia_i \le x \le b_i), where 1≤ai≤bi≤10181 \le a_i \le b_i \le 10^{18}. Moreover, for every ii with 2≤i≤n2 \le i \le n, ai≥bi−1+2a_i \ge b_{i-1} + 2 (so the intervals are pairwise disjoint and given in increasing order).

Output

Represent each equivalence class as the concatenation of its digits in ascending order.

Print all equivalence classes of the digits 11 through 99, one per line, sorted lexicographically.

Examples2

  1. Example 1

    Input
    1
    1 566
    
    Expected output
    1234
    5
    6
    789
    
  2. Example 2

    Input
    1
    30 75
    
    Expected output
    12
    345
    6
    7
    89