Let σ be a permutation of { 1 , 2 , 3 , … , 1 0 0 } . Then there exists a smallest positive integer f ( σ ) such that σ f ( σ ) = id , where id is the identity permutation.
What is the largest value of f ( σ ) over all σ ?
Note:
σ
k
is
σ
applied
k
times in succession.
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.
Relevant wiki: Dynamic Programming
A permutation σ is a product of disjoint cycles. Suppose the cycles of σ have length a 1 , a 2 , a 3 , … , a n . Then f ( σ ) = lcm { a 1 , a 2 , a 3 , … , a n } , because in order to set the i -th cycle to be the identity, we need to raise σ to the a i -th power. Thus our goal is to maximize lcm { a 1 , a 2 , a 3 , … , a n } , given the constraint that a 1 + a 2 + a 3 + … + a n = 1 0 0 (because the cycles are disjoint).
Suppose a i = p q where p , q > 1 are relatively prime (that is, a i is not the power of a prime). Then p + q ≤ a i and lcm { p , q } = a i , so we may substitute the cycle with length a i with several cycles: one of length p , one of length q , and remaining cycles of length 1 to make the number of elements equal. For example, if σ has a cycle of length 12, then we can split it off into a cycle of length 4, another cycle of length 3, and five cycles of length 1. This will give the same LCM, and hence same value of f . Thus doing this will never make f to decrease, and so we may assume that all a i 's are powers of prime.
Now, if there are a i , a j such that a i ∣ a j and a i > 1 , then we may replace the cycle with length a i with a i cycles of length 1, because the LCM is not changed ( lcm { a i , a j } = a j ). Thus we may also assume all a i 's are relatively prime. Now, we have several powers of primes, all relatively prime, which add up to 100. The LCM is simply the product of all of them, so we need to maximize this product.
We can now use dynamic programming to solve this. Label the primes p 0 , p 1 , p 2 , … in increasing order. We generalize the problem: let s o l v e ( s , k ) to be the maximum product of a 1 , a 2 , … , a n , given that a 1 + a 2 + … + a n = s , each a i is the perfect power of a prime p ≥ p k , and any two a i 's are relatively prime. For example, s o l v e ( 5 , 0 ) = 6 , with a 1 = 2 , a 2 = 3 , but s o l v e ( 5 , 1 ) = 5 with a 1 = 5 , because we may not use p 0 = 2 . (And s o l v e ( 5 , 3 ) = 1 , since we may not use any of the primes 2, 3, 5, so we're stuck with setting a 1 = a 2 = … = a 5 = 1 .) Our goal is to compute s o l v e ( 1 0 0 , 0 ) .
The base case is if s < p k . We cannot use any prime at all, so we can immediately conclude s o l v e ( s , k ) = 1 by setting all a i to 1. We can now make use of recursion: in order to compute s o l v e ( s , k ) , we will take the maximum among the following:
The correctness is easily seen, because this recursion is actually testing all possible combinations: either we ignore p k , or one of the a i 's is a power of p k . This recursion will end, because each successive call increases k and never increases s ; thus at some point, we will compute s o l v e ( s ′ , k ′ ) where p k ′ > s ≥ s ′ and thus we hit the base case. Finally, this will take at most O ( lo g s s 2 ) time: there are O ( lo g s s ) primes below s , so there are that many values for k , and there are O ( s ) values for s . By memoizing computation, no call will be computed more than once.
The following is a Python 3 implementation of the above algorithm.
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 |
|
Alternatively, we can look up the value of Landau's function g ( 1 0 0 ) , which is exactly what we want...
Log in to reply
I didn't know it has a name. But this gives a way to compute the values of Landau's function. Not necessarily the fastest, but it's pretty reasonable.
Log in to reply
Here is my Delphi code - very quick. (Delphi does not have an in-built integer power function with native integer output, so I rolled my own)
procedure TForm1.Button5Click(Sender: TObject);
const
primes: array[1..25] of Integer = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67,71, 73, 79, 83, 89, 97);
function GetM(a,b: Integer): LongInt;
var
u: Integer;
begin
if b = 1 then
begin
Result := 1;
while primes[1] * Result <= a do Result := Result * primes[1];
end
else
if a = 0 then
Result := 1
else
begin
Result := GetM(a,b-1);
u := 1;
while primes[b]*u <= a do
begin
u := primes[b]*u;
Result := Max(Result,u*GetM(a-u,b-1));
end;
end;
end;
begin
Label1.Caption := IntToStr(GetM(100,25));
end;
Log in to reply
@Mark Hennings – Yes, that's the same algorithm as mine.
Log in to reply
@Ivan Koswara – You seem to be using memory to avoid repeat calls. My recursive routine GetM(a,b) only calls GetM(c,d) for values c,d with c + d < a + b , so there cannot be any repeat.
Log in to reply
@Mark Hennings – There can still be some repeat; for example, when you compute G e t M ( 5 , 3 ) , one path goes G e t M ( 5 , 3 ) → G e t M ( 5 , 2 ) → G e t M ( 2 , 1 ) → G e t M ( 0 , 0 ) (taking no 5, one 3, and one 2), while another goes G e t M ( 5 , 3 ) → G e t M ( 0 , 2 ) → G e t M ( 0 , 1 ) → G e t M ( 0 , 0 ) (taking one 5, no 3, and no 2).
...okay, that explicit example might be wrong because you immediately cut down computation when a = 0 or b = 1 , but it's definitely possible to generalize it; say, G e t M ( 2 2 , 5 ) will compute G e t M ( 2 , 1 ) twice.
Log in to reply
@Ivan Koswara – Agreed, since 7 + 1 1 = 5 + 1 3 , GetM(26,6) will call GetM(8,2) more than once.
However, since my routine takes less than a millisecond to run, with no change to GetTickCount, I am not too worried about performance.
Log in to reply
@Mark Hennings – Yes, when I wrote my first program to solve this, I didn't use memoization and it went fast enough as well. (Actually, it was about 0.5 seconds on Python; I'm not sure why it was pretty slow, but for the purpose of the problem that's fast enough.) It's only when I wrote the solution that I found using memoization simplified the time complexity analysis.
Log in to reply
@Ivan Koswara – Python vs. Delphi: Sounds like the difference between interpreted code and compiled code.
I suspect that maintaining the memory list - particularly searching for entries - will have a performance impact after eventually.
Log in to reply
@Mark Hennings – It's possible that part of it is because of Python being interpreted, but I have my rule of thumb that approximately 1 0 6 operations can be done in one second on my computer ( 1 0 7 on online judges like Codeforces and such, because my computer is slow and online judges are fast), and the analysis above says that it takes only about 2 5 0 0 × constant operations. I believe the constant here is small (around 10), so it doesn't explain needing 0.5 seconds.
The memory list is a dictionary, and so is a hash table. Assuming no collisions, lookups should be constant time.
Huh. The primes from 2 to 23 conveniently sum to 100. Why isn't their product the maximum we can achieve?
Log in to reply
Because cycles of length 2 , 3 , 2 3 reach the identity permutation faster than cycles of length 1 6 , 9 , 1 , 1 , 1 . We can replace 2 , 3 , 2 3 with 1 6 , 9 , 1 , 1 , 1 to obtain a higher f .
The last part can actually be done by hand or with just a single loop. The idea is to iterate up from 1. Every time you see a prime or a power of a prime you add it into the list, if the list already contains a lower power of that prime you remove it. Iteration continues until the sum of the list elements is less than 100 (or any K you want to calculate it for), the number causing an overflow is not included. So it is like: {2} -> sum=2 {2,3} -> s=5 {3,4} -> s=7 {3,4,5} -> s=12 ... {5,7,9,11,13,16,17,19} -> s=97 {5,7,9,11,13,16,17,19,23} -> s=120
Problem Loading...
Note Loading...
Set Loading...
This is Landau's function g ( 1 0 0 ) . See e.g. here or here .
As already very well explained by Ivan, any permutation of n objects can be decomposed in cycles. The sum of the lengths of the cycles must give n and each cycle gives the identity if repeated as many times as its length. For example, consider the following permutation:
σ = ( 2 5 4 3 1 1 2 3 4 5 ) = ( 1 2 5 ) ( 3 4 )
It is made up of a 3-cycle and a 2-cycle. The 3-cycle must be applied 3 times in order to give the identity whereas the 2-cycle has to be applied 2 times, as can be easily checked. Therefore, the full permutation σ must be applied 6 times, i.e. the LCM of 2 and 3, in order to give the identity.
Therefore, let's find a set of number which summed give 100 and whose LCM is the maximum possible. Using prime numbers is a good start, because their LCM is their product. Indeed, it can be shown that the product of the first m primes (the primorial) is a lower bound of Landau's function (for a suitable m ). Note that:
2 + 3 + 5 + 7 + 1 1 + 1 3 + 1 7 + 1 9 + 2 3 = 1 0 0
And thus
LCM ( 2 , 3 , 5 , 7 , 1 1 , 1 3 , 1 7 , 1 9 , 2 3 ) = 2 ⋅ 3 ⋅ 5 ⋅ 7 ⋅ 1 1 ⋅ 1 3 ⋅ 1 7 ⋅ 1 9 ⋅ 2 3 = 2 2 3 0 9 2 8 7 0
This was my first guess, and it is wrong because it can be improved. Indeed, what counts in order to maximize the LCM is to have coprime numbers, not necessarily primes. We can improve the above result substituting 2 + 2 3 by 1 6 + 9 because in this case we have a factor 9 ⋅ 1 6 = 1 4 4 in the LCM rather than 2 ⋅ 3 ⋅ 2 3 = 1 3 8 . This gives:
LCM ( 3 , 5 , 7 , 9 , 1 1 , 1 3 , 1 6 , 1 7 , 1 9 ) = 5 ⋅ 7 ⋅ 9 ⋅ 1 1 ⋅ 1 3 ⋅ 1 6 ⋅ 1 7 ⋅ 1 9 = 2 3 2 7 9 2 5 6 0
which is the correct answer.