Find the kth eleven-non-free number

前端 未结 8 2275
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 10:10

Let\'s define eleven-non-free numbers:

If we consider a number as a string, then if any substring inside is a (non-zero) power of 11, then this

8条回答
  •  没有蜡笔的小新
    2021-02-04 10:41

    10^18 is really, really big. Brute force is not your friend.

    However, let us note some conveniences:

    • If a string s represents a eleven-non-free number, then trivially so does d s (where d is a string of digits).
    • If you wanted to know how many k-digit numbers are eleven-non-free, you could base that off of how many k-1-digit numbers are eleven-non-free.
    • (e.g. how many 1xx numbers are eleven-non-free? Well, how many xx numbers are eleven-non-free? Obviously at least that many 1xx numbers are eleven-non-free.. The only additional cases are when a power of eleven starts with the digit at the start -- with the 1 that just got tacked on the front.)
    • This suggests a relatively straightforward dynamic programming approach to figure out how many eleven-non-free numbers are in a string of known length with a fixed prefix. (e.g. how many eleven-non-free numbers are in 934xxxxx)

    Finally, throw some binary-search style logic on top, and you should be able to find exactly which number is the N-th eleven-non-free number.

提交回复
热议问题