Definiujemy jako listę odrębnych potęg które sumują się do . Na przykład .2 x V ( 35 ) = [ 32 , 2 , 1 ]
Zgodnie z konwencją, moce są tutaj sortowane od najwyższych do najniższych. Ale nie wpływa to na logikę wyzwania ani oczekiwane rozwiązania.
Zadanie
Biorąc pod uwagę półpierwszą literę , zamień każdy termin w inną listę potęg które sumują się do tego terminu, w taki sposób, że suma wszystkich powstałych podlist stanowi dokładne pokrycie macierzy zdefiniowanej jako:V ( N ) 2 M.
gdzie i są głównymi czynnikami .Q N
Jest to o wiele łatwiejsze do zrozumienia na niektórych przykładach.
Przykład 1
Dla mamy:
- i
- i
Aby zmienić w dokładną osłonę , możemy podzielić na i na , podczas gdy pozostanie niezmienione. Możliwe wyjście to:
Innym ważnym wyjściem jest:
Przykład nr 2
Dla mamy:
- i
- and
A possible output is:
Rules
- Because factorizing is not the main part of the challenge, you may alternately take and as input.
- When several possible solutions exist, you may either return just one of them or all of them.
- You may alternately return the exponents of the powers (e.g. instead of ).
- The order of the sub-lists doesn't matter, nor does the order of the terms in each sub-list.
- For some semiprimes, you won't have to split any term because already is a perfect cover of (see A235040). But you still have to return a list of (singleton) lists such as for .
- This is code-golf!
Test cases
Input | Possible output
-------+-----------------------------------------------------------------------------
9 | [ [ 4, 2, 2 ], [ 1 ] ]
15 | [ [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
21 | [ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]
51 | [ [ 32 ], [ 16 ], [ 2 ], [ 1 ] ]
129 | [ [ 64, 32, 16, 8, 4, 2, 2 ], [ 1 ] ]
159 | [ [ 64, 32, 32 ], [ 16 ], [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
161 | [ [ 64, 32, 16, 16 ], [ 8, 8, 4, 4, 4, 2, 2 ], [ 1 ] ]
201 | [ [ 128 ], [ 64 ], [ 4, 2, 2 ], [ 1 ] ]
403 | [ [ 128, 64, 64 ], [ 32, 32, 16, 16, 16, 8, 8 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
851 | [ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
2307 | [ [ 1024, 512, 512 ], [ 256 ], [ 2 ], [ 1 ] ]
Odpowiedzi:
K (ngn/k),
6663 bytesTry it online!
accepts (P;Q) instead of N
algorithm:
compute A as the partial sums of V(P*Q)
multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B
find the positions of those elements in B that also occur in A; cut R right after those positions
źródło
Jelly, 24 bytes
A monadic link accepting a list of two integers
[P, Q]
which yields one possible list of lists as described in the question.Try it online! (footer prints a string representation to show the list as it really is)
Or see the test-suite (taking a list of N and reordering results to be like those in the question)
How?
We may always slice up the elements ofM from lowest up, greedily (either there is a 1 in M or we had an input of 4 , when M=[[4]] ) in order to find a solution.
Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.
źródło
Python 2,
261233232231 bytesTry it online!
1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.
Takes as input
p,q
. Pursues the greedy algorithm.źródło
-k-1
can be~k
.i,j
assignment can bei,j=[i+1,i,0,j+1][j+1<len(v)::2]
for -1 bytewhile v[j]>-m
can bewhile-m<v[j]
Jelly, 41 bytes
Try it online!
Should probably be much shorter (some parts feel very repetitive; especially[P,Q] .
ÇIP$Ƈ
, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input asźródło
Jelly, 34 bytes
Try it online!
Input format:
[P, Q]
(the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).Output format: List of all solutions (shown as grid representation of 3D list over TIO).
Speed: Turtle.
źródło
Pyth, 27 bytes
Inspired by Jonathan Allan's crushing of my Jelly solution. TakesN as input.
Try it here!
źródło
Haskell,
281195 bytesźródło
(==)
, użyj1>0
zamiastTrue
i nie używajwhere
. Równieżn'
można skrócić .. Dzięki temu można zaoszczędzić 72 bajtów: Spróbuj online!