n * k = dd0d00d其中d =…?


14

给定正整数n≤500

  • 找出最小的正整数k,以使n * k的十进制表示形式中的所有数字均为0d且1≤d≤9

  • 在不到30秒的时间内打印或返回d(有关“说明和规则”部分的详细信息,请参阅d)。

简单的例子

这是d的前30个值。

+----+-------+---------+---+    +----+-------+---------+---+
|  n |     k |   n * k | d |    |  n |     k |   n * k | d |
+----+-------+---------+---+    +----+-------+---------+---+
|  1 |     1 |       1 | 1 |    | 16 |     5 |      80 | 8 |
|  2 |     1 |       2 | 2 |    | 17 |   653 |   11101 | 1 |
|  3 |     1 |       3 | 3 |    | 18 |     5 |      90 | 9 |
|  4 |     1 |       4 | 4 |    | 19 |   579 |   11001 | 1 |
|  5 |     1 |       5 | 5 |    | 20 |     1 |      20 | 2 |
|  6 |     1 |       6 | 6 |    | 21 |    37 |     777 | 7 |
|  7 |     1 |       7 | 7 |    | 22 |     1 |      22 | 2 |
|  8 |     1 |       8 | 8 |    | 23 |  4787 |  110101 | 1 |
|  9 |     1 |       9 | 9 |    | 24 |    25 |     600 | 6 |
| 10 |     1 |      10 | 1 |    | 25 |     2 |      50 | 5 |
| 11 |     1 |      11 | 1 |    | 26 |    77 |    2002 | 2 |
| 12 |     5 |      60 | 6 |    | 27 |    37 |     999 | 9 |
| 13 |    77 |    1001 | 1 |    | 28 |    25 |     700 | 7 |
| 14 |     5 |      70 | 7 |    | 29 | 37969 | 1101101 | 1 |
| 15 |     2 |      30 | 3 |    | 30 |     1 |      30 | 3 |
+----+-------+---------+---+    +----+-------+---------+---+

不太容易的例子

这一挑战的一个特殊性是,某些价值比其他价值难得多-至少采用纯粹的蛮力方法。以下是一些导致k值较高的n的示例。

+-----+------------+---------------+---+    +-----+------------+---------------+---+
|   n |          k |         n * k | d |    |   n |          k |         n * k | d |
+-----+------------+---------------+---+    +-----+------------+---------------+---+
|  81 |   12345679 |     999999999 | 9 |    | 324 |   13717421 |    4444444404 | 4 |
| 157 |   64338223 |   10101101011 | 1 |    | 353 |   28615017 |   10101101001 | 1 |
| 162 |   13717421 |    2222222202 | 2 |    | 391 |  281613811 |  110111000101 | 1 |
| 229 |   43668559 |   10000100011 | 1 |    | 405 |   13717421 |    5555555505 | 5 |
| 243 |   13717421 |    3333333303 | 3 |    | 439 |   22781549 |   10001100011 | 1 |
| 283 |   35371417 |   10010111011 | 1 |    | 458 |   43668559 |   20000200022 | 2 |
| 299 |   33478599 |   10010101101 | 1 |    | 471 |   64338223 |   30303303033 | 3 |
| 307 |   32576873 |   10001100011 | 1 |    | 486 |   13717421 |    6666666606 | 6 |
| 314 |   64338223 |   20202202022 | 2 |    | 491 |  203871711 |  100101010101 | 1 |
| 317 | 3154574483 | 1000000111111 | 1 |    | 499 |   22244489 |   11100000011 | 1 |
+-----+------------+---------------+---+    +-----+------------+---------------+---+

澄清和规则

  • n * k将始终至少包含一位数字 d,但可能根本不包含零。
  • 这是,因此以字节为单位的最短代码获胜。但是,您的程序或函数必须能够在不到30秒的时间内在中等范围的硬件上返回任何1≤n≤500的结果。
  • 请记住,有些价值比其他价值难找。试图强行k值的程序不太可能符合时间限制(好的测试用例是n = 317)。有明显更快的方法来找到d

参考表

的所有值d1≤N≤500列出如下。

n       | d
--------+--------------------------------------------------
001-025 | 1 2 3 4 5 6 7 8 9 1 1 6 1 7 3 8 1 9 1 2 7 2 1 6 5
026-050 | 2 9 7 1 3 1 8 3 2 7 9 1 2 3 4 1 6 1 4 9 2 1 6 7 5
051-075 | 3 4 1 9 5 7 1 2 1 6 1 2 9 8 5 6 1 4 3 7 1 9 1 2 3
076-100 | 4 7 6 1 8 9 2 1 4 5 2 3 8 1 9 1 4 3 2 5 6 1 7 9 1
101-125 | 1 6 1 8 7 2 1 9 1 1 1 7 1 2 5 4 9 2 7 6 1 2 3 4 5
126-150 | 6 1 8 3 1 1 6 7 2 9 8 1 6 1 7 1 2 1 9 5 2 7 4 1 3
151-175 | 1 8 9 7 5 4 1 2 1 8 7 2 1 4 3 2 1 8 1 1 3 4 1 6 7
176-200 | 8 3 2 1 9 1 2 1 8 5 6 1 4 9 1 1 6 1 2 3 7 1 9 1 2
201-225 | 3 2 7 4 5 2 9 8 1 7 1 4 1 2 5 9 7 2 3 2 1 2 1 7 9
226-250 | 2 1 4 1 1 3 8 1 6 5 4 3 7 1 6 1 2 3 4 7 6 1 8 3 5
251-275 | 1 6 1 2 3 8 1 6 7 2 9 2 1 6 5 7 3 4 1 9 1 8 3 2 5
276-300 | 6 1 2 9 7 1 2 1 4 5 2 7 9 1 1 3 4 1 7 5 8 9 2 1 3
301-325 | 7 2 3 8 5 6 1 4 3 1 1 8 1 2 9 4 1 2 1 8 1 7 1 4 5
326-350 | 2 1 8 7 3 1 4 3 2 5 8 1 2 3 2 1 6 1 8 3 2 1 4 1 7
351-375 | 9 8 1 6 5 4 7 2 1 9 1 2 3 4 5 2 1 8 9 1 7 6 1 2 3
376-400 | 8 1 9 1 2 3 2 1 6 7 2 9 4 1 3 1 7 1 2 5 9 1 2 7 4
401-425 | 1 6 1 4 5 2 1 8 1 1 3 4 7 9 5 8 1 2 1 6 1 2 3 8 5
426-450 | 2 7 4 3 1 1 9 1 7 3 4 1 6 1 4 3 2 1 4 5 2 3 7 1 9
451-475 | 1 4 3 2 5 8 1 2 9 2 1 6 1 8 3 2 1 6 7 1 3 8 1 6 5
476-500 | 7 3 2 1 6 1 2 3 4 5 6 1 8 3 7 1 6 1 2 9 8 7 6 1 5

1
最近挑战的启发(但有很大不同)。
Arnauld

n = 6669666-> d = 9
J42161217

那张桌子上有趣的对角线。
詹姆斯

@詹姆斯确实。通过设置MOD 24的格式,图案会显得更加清晰。使用MOD 25,我们得到了一些对角线。:-)
Arnauld

Answers:


3

果冻16 15 14字节

²B€Ḍ9×þF%Þ¹ḢQS

二次运行时间(在TIO上不到25秒)。

在线尝试!

备用版本,15字节

2ȷB€Ḍ9×þF%Þ¹ḢQS

持续运行时间(在TIO上大约1秒)。

在线尝试!

怎么运行的

²B€Ḍ9×þF%Þ¹ḢQS  Main link. Argument: n

²               Take the square of n.
                This bound is high enough for all integers up to 500. 
                In fact, The highest value we need is 1387 for input 471, so
                2000 (2ȷ) is also enough (and a lot faster).

 B€             Binary; convert 1, ..., 4159 to base 2.
   Ḍ            Undecimal; convert each digit array from base 10 to integer.
                This generates the array A of all positive integers up to n²
                whose decimal representations consist entirely of 1's and 0's.
    9×þ         9 multiply table; for each x in A, yield [x, 2x, ..., 8x, 9x].
       F        Flatten; concatenate the resulting arrays, yielding the vector
                V. Note that V contains all numbers that match the regex ^d[0d]*$
                in base 10, in ascending order.
          ¹     Identity; yield n.
        %Þ      Sort the entries for V by their remainders modulo n. This places
                multiples of n at the beginning. The sorting algorithm in stable,
                so the first element of sorted V is the smallest multiple of n.
           Ḣ    Head; extract the first element.
            Q   Unique; deduplicate its digits in base 10. This yields [d, 0].
             S  Take the sum, yielding d.

5

JavaScript(ES6),83个字节

n=>{for(p=1;;p=k)for(d=0;d++<9;)for(k=p;k<p+p;k++)if(k.toString(2)*d%n<1)return d;}

现在返回6n=252!我尝试了一种递归方法,但它也是83个字节,对于较难的数字我崩溃了:

f=(n,p=1,d=1,k=p)=>k<p+p?k.toString(2)*d%n<1?d:f(n,p,d,k+1):d>8?f(n,p+p):f(n,p,d+1)

4

Mathematica,103 100 97字节

#&@@IntegerDigits[Sort[Join@@Table[Cases[FromDigits/@{0,i}~Tuples~13/#,_Integer],{i,9}]][[10]]#]&


在0.39秒内找到317

在线尝试 复制/粘贴代码,在末尾添加[317],然后按Shift + Enter键运行

来自@JungHwan Min的
-3个字节来自@Keyu Gan的-3个字节


你可以摆脱**#,并且Tuples[{0,i},13]{0,i}~Tuples~13
JungHwan敏

是的,当然。完成!
J42161217

哦,还有另一个:[[1]]结尾#&@@处与开头处相同
JungHwan Min

...我们达到了100!感谢-3个字节
J42161217 '17

您可以使用Join@@,而不是Flatten@
科宇甘

2

蟒2/3,129个 128 127字节

from itertools import*
lambda n:next(d for p in count()for d in range(1,10)for k in range(2**p,2*2**p)if d*int(bin(k)[2:])%n<1)

-1个字节:count(0)count()
-1个字节:==0<1因为不能为负


2

果冻,21 字节

9Rṭ€0ṗ€⁴ẎḌḍ@Ðf⁸ṢDFḟ0Ḣ

返回该数字的单子链接或打印该数字的完整程序。

有限范围蛮力柜子这需要小于20秒的任何1≤N≤500(小于3秒,对于1个字节码的成本-替换13)。

在线尝试!

怎么样?

9Rṭ€0ṗ€⁴ẎḌḍ@Ðf⁸ṢDFḟ0Ḣ - Link: number, n
9R                    - range of 9 = [1,2,3,4,5,6,7,8,9]
  ṭ€0                 - tack €ach to 0 -> [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9]]
       ⁴              - literal 16
     ṗ€               - Cartesian product for €ach
        Ẏ             - tighten (flatten by 1 level)
         Ḍ            - covert from decimal list to number (vectorises)
              ⁸       - chain's left argument (n)
            Ðf        - filter keep items for which this yields a truthy value:
          ḍ@          -   divisible? with swapped @rguments
               Ṣ      - sort
                D     - convert to decimal list (vectorises)
                 F    - flatten into a single list
                  ḟ0  - filter out zeros
                    Ḣ - head (get the first value)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.