l-_-l's blog

By l-_-l, history, 13 months ago, translation, In English

And here is the Editorial!

We hope you liked the problems. Thanks everyone!

\[^-^]/

2091A - Olympiad Date

Tutorial
Solution

2091B - Team Training

Tutorial
Solution

2091C - Combination Lock

Tutorial
Solution

2091D - Place of the Olympiad

Tutorial
Solution

2091E - Interesting Ratio

Tutorial
Solution

2091F - Igor and Mountain

Tutorial
Solution

2091G - Gleb and Boating

Tutorial
Solution
  • Vote: I like it
  • +69
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
Rev. 2  
Vote: I like it +4 Vote: I do not like it

Problem D O(1)

void solve(){

ll n,m,k;

cin>>n>>m>>k;

ll total=(n*m-k)/n;

ll kena=m-total;

cout<<(ll)ceil((float)kena/(float)(total+1))<<endl;

}

int main(){

ios::sync_with_stdio(false);

cin.tie(0);

int t=1;

cin>>t;

while (t--)

{

    solve();
}

}

»
13 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

F has a nice explanation

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    can you please help me.

    In F while selecting second hold on same row, it is done

    dp[i][j][f]= dp[i][j][f]+∑j+dy=j−d dp[i][y][1]−dp[i][j][1]

    But why here dp[i][y][1]? shouldn't it be dp[i][y][0]? why we take another hold if we already took 2?

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      If we used dp[i][y][0], it would mean we are still in the state where only one hold is selected, which contradicts the fact that we are selecting the second hold at y.

      We need to transition to the state where two holds are used, which is captured by dp[i][y][1].

»
13 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Praise the sun! Solaire posture

»
13 months ago, hide # |
Rev. 3  
Vote: I like it +8 Vote: I do not like it

In G, I wasn't able to prove the fact where the answer will always be $$$k$$$ or $$$k - 2$$$ for $$$s \ge k^2$$$, so I am posting my solution for the same.

Main thing to note is that for each value of power $$$x$$$, we only need to consider $$$x$$$ different residues modulo $$$x$$$, and particularly for each turn around step we only need to consider the maximum $$$x$$$ values we can reach if we are travelling forwards or the minimum $$$x$$$ values if we are travelling backwards. Running complexity is $$$O(k^2)$$$ for each valid $$$k$$$ resulting in overall complexity of $$$\begin{equation} \sum_{i=1}^{k} i^2 = O\left(\frac{k(k+1)(2k+1)}{6}\right) \end{equation}$$$

Edit: My Submission — 312461704

»
13 months ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

What is the reason behind of making k <= 1000 and sum of k <= 2000 in G? I just didn't write the code because I have never wrote an n^3 algorithm for n <= 1000. And yes it has very nice constant factor and in practice works very fast. But why couldn't u make it like k <= 800, sum <= 800, any unintended solutions that pass under smaller constraints?

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it -8 Vote: I do not like it

    When it comes to this kind of question it is safe to guess the conclusion, we are almost certain that not all $$$k$$$ will be traversed, and ultimately there must be very few valuable $$$k$$$, see my submission 312426354, where we note that the run time is very short.

    Edit: I think this problem may be related to the harmonic series, which has only about $$$\log{k}$$$ valid numbers.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Cool contest!

In fact there are two ways to approach the problem $$$F$$$: either by counting ways to reach the current 'X' index from the neighboring 'X' indices or vice versa, counting ways to reach the neighboring 'X' indices from the current. The first approach can be implemented with prefix sums and the second via a difference array.

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    Yes, if we consider where the dp state can transition to, we can use the difference array to solve it.

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +14 Vote: I do not like it

In problem G I just "felt" the algorithm wouldn't run slow, then I submitted the code and got AC.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Interesting G.

»
13 months ago, hide # |
 
Vote: I like it +7 Vote: I do not like it

Using derivatives to prove the $$$\Theta$$$ of G may be more intuitive&simple (⑅˃◡˂⑅)

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Thanks for a great contest!!!

»
13 months ago, hide # |
Rev. 2  
Vote: I like it -12 Vote: I do not like it

As per my current rating, of 1076, If I have solved questions till E, how much will the rating change ?

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +1 Vote: I do not like it

Problem D

#include <stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    int n, m, k, groups, num;
    while (t--)
    {
        scanf("%d%d%d", &n, &m, &k);
        n = (k - 1) / n + 1;        // max num per row (ceil)
        groups = m - n + 1;         // m = (group - 1) + n;
        num = (n - 1) / groups + 1; // num per group (ceil)
        printf("%d\n", num);
    }
}
»
13 months ago, hide # |
Rev. 2  
Vote: I like it +6 Vote: I do not like it

Problem D O(1)

void solve(){
    int n , m , k;
    cin >> n >> m >> k;
    int ans = m / (( n * m - k) / n + 1);
    cout << ans <<endl;
}
»
13 months ago, hide # |
Rev. 6  
Vote: I like it +14 Vote: I do not like it

Can G problem be solved in $$$O(K^2log(K))$$$
My Solution: Submission

Consider that $$$s \lt k*k$$$

  • It can be observed that the visitable points form continuous ranges. I try to maintain the visited points as ranges $$$[lx , rx]$$$.
  • My claim: number of continuous visited ranges (segments) are <= k, after every turn.
  • Reason: Say power was $$$x$$$ , so adjacent visitable ranges have atmost x gap. So for the next turn with power $$$x-1$$$ the adjacent ranges would definitely merge, new ranges are created only at the boundary points.
  • Propagating the intervals to any multiple of x can be done using sliding window.
  • I am bad at explanation, Correct me if i am wrong anywhere.
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can any one explain another approach for problem f ?

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

in d part for the binary search suose you wrote while(l<=r) you get TLE error ,can someone tell me the calculation why ?

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anyone please explain me in problem-G; when s=10 and k=4 how is the answer 2

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    • $$$power=4$$$, move to $$$x=8$$$
    • $$$power=3$$$, move to $$$x=8-6=2$$$
    • $$$power=2$$$, move to $$$x=2+8=10$$$
»
13 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Some how I find C is the most difficult one among A-F, I have read the editorial, but can not I find a way to come up with solution, why people can relate to the fact that even length can not be constructed in the first place then gradually come up with the solution?

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Don't forget to praise the moon!

»
13 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it
F using DP+prefixSum + difference array
»
13 months ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

O(k^2logk) solution for G.

For convenience, we will only consider the round of jumping to the right here. The turn that jumps to the left is symmetrical. If the current jump distance is k0, then for each modulo result of k0, we only need to record the smallest position. Because smaller positions can always jump to larger positions. Next, consider turning at each position. These positions, such as d, d+k0, and d+2k0, must form a cyclic interval in the form of k0-1. What we need to do is to take the maximum value of the cyclic interval for the arithmetic sequence. It can be achieved using a suitable data structure.

  • »
    »
    13 months ago, hide # ^ |
    Rev. 4  
    Vote: I like it 0 Vote: I do not like it

    I implemented a similar solution with same complexity. 312517599

    • My idea was to maintain all visitable points as continuous ranges $$$[lx,rx]$$$, it can be easily shown that count of such intervals is atmost k, and after updating for every turn, atmost 1 new range (near boundary) gets added to my datastructure.

    • Processing K intervals for each turn (k , k-1 , ... 1) with sorting yeilds the same time complexity

    • If last range is of form $$$[lx , S]$$$ then this turn's power is the maximal possible answer.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem-F is wrong, you have not calculated the possibility of Igor climbing from level x to level x+2,x+3,....x+n directly, If Igor can't skip any levels while climbing from one level to another you should have clearly mentioned this in problem statement ;(

»
13 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

I think I have a proof that my solution for G (which is basically the same as the editorial solution, but with dynamic programming instead of BFS) works in $$$O(k^2)$$$ per test case.

Let $$$x$$$ be the maximum integer such that $$$s \ge x \cdot k$$$. In other words, $$$x$$$ is $$$\frac{s}{k}$$$, rounded down.

Each value of Gleb's power is processed in $$$O(x \cdot k)$$$, and I claim that after we decrease Gleb's power by $$$O(\frac{k}{x})$$$, we will find the answer. So, if this claim is true, it works in $$$O(x \cdot k \cdot \frac{k}{x}) = O(k^2)$$$.

Let's prove this claim. Initially, Gleb is at point $$$0$$$ and has power $$$k$$$. Let's consider the points where he can be after we decrease his power by $$$2$$$. If he moves $$$i$$$ times to the right, makes a U-turn, moves $$$i$$$ times to the left, and makes a turn again, he will be in point $$$i$$$. And the maximum possible value of $$$i$$$ is $$$x$$$, so, after we decrease Gleb's power by $$$2$$$, he can be in any point from $$$[1, x]$$$ (and possibly some other points we haven't considered yet).

Using a similar argument, we can prove that after we decrease his power by $$$2$$$ again, Gleb can be in any point from $$$[2, 2x-1]$$$, and so on. So, after $$$2i$$$ power decreases, there is a segment of length very close to $$$i \cdot x$$$ in which every point is reachable by Gleb with his current power. As soon as this segment's length becomes equal or greater than his current power $$$k-2i$$$, there will be at least one point $$$p$$$ in this segment such that $$$p \bmod (k-2i) = s \bmod (k-2i)$$$, so the point $$$s$$$ will be reachable with current power. If we take $$$i = \frac{k}{x}$$$, the length of the segment will be very close to $$$x \cdot \frac{k}{x} = k$$$.

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    I wonder why after Gleb decrease his power by 4, he can only be in any point from $$$[2, 2x - 1]$$$ instead of $$$[2, 2x]$$$

    As given that $$$s ≥ x * k$$$, then $$$(s - 2x) ≥ x * (k - 2)$$$, he should have the chance to move another $$$x$$$ times

    In that case, then after $$$k / x$$$ turn, We should at least get from $$$[k / x, k]$$$, and the power should be $$$k - 2 * k / x$$$, we may not seem to prove Gleb can get to every place from $$$[0, k - 2 * k / x - 1]$$$, am i making some mistakes somewhere?

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      Yeah, you are right, it looks like it is possible to move another $$$x$$$ times after we decrease the power by $$$2$$$, so the segment will be $$$[2, 2x]$$$ after decreasing the power by $$$4$$$. So after $$$2i$$$ decreases, our segment will be $$$[i, i \cdot x]$$$, and as soon as $$$i$$$ becomes at least $$$2 \cdot \frac{k}{x-1}$$$, there will be at least $$$k$$$ consecutive points we can be in.

      These points don't have to start from $$$0$$$, since any segment of $$$k$$$ consecutive points contains all remainders modulo $$$k$$$.

»
13 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

I'm truly dissapointed by the testcases on G , if I would have got a wrong answer I would have changed the range from 0 to n instead of 1 to n.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I got TLE on system testing for problem E just because of j=1. How?! (j=2 got AC)

Code
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

There is no way 63% of people that solved problem A, can actually solve problem C. I find problem C from this contest (division 3 contest) to be much harder than problem A from the previous division 1 contest (Codeforces Round 1012). The mentioned problem A was solved only by 935 persons, this problem C was solved by 15434 persons. Also, not to mention the difference in ello/rating between people that participated in the division 1 contest vs the rating of the people that participated in the division 3 contest. (In other words, the persons with higher rating can't solve the easier problem, but persons with lower rating can solve the harder problem).

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Div. 1 A is supposed to be harder than Div. 3 C. In this case, I think that A from 1012 Div. 1 is much harder than 1013 Div. 3 C. Furthermore, Div. 2 contests have way more participation than Div. 1 contests, so the early problems are going to have more solves.

    • »
      »
      »
      13 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      if you say that: this C problem is much easier (for you) than the mentioned div 1 A problem.

      Than how can you logically solve it (without guessing a pattern) ?

      Div 1 A is actually very easy, you just find a prime number around n/2, than you add numbers in the sequence/permutation in such a way that: every 2 numbers from permutation the average stays equal with the prime number that you found (around n/2). This problem has a clear logic, you don't need to guess any pattern or use any advanced mathematics.

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        In Div. 3 C it is pretty clear to prove that n must divide 1+2+...+n, so n must be odd. Divisibility is not advanced mathematics. And reversing a permutation is much easier to find than the more advanced algorithm of sum pairing needed for Div. 1 A.

        • »
          »
          »
          »
          »
          13 months ago, hide # ^ |
          Rev. 2  
          Vote: I like it +5 Vote: I do not like it

          I have no idea why i need to prove "n must divide 1+2+...+n".

          Even if i guess the fact that correct permutations exist only when n is odd, i still have no idea how you can construct a correct permutation without guessing the correct permutation/pattern (without clear logic).

          i found 3 different patterns that work, but i have no idea why any of them work.

          pattern 1: https://mirror.codeforces.com/contest/2091/submission/312558837

           for (ui i = 0; i < n; ++i) {
          			a[i] = ((2 * i) % n) + 1;
          		}
          

          pattern 2: https://mirror.codeforces.com/contest/2091/submission/312559065

          for (int i = n; i > 0; i--) {
          		std::cout << i << ' ';
          	}
          

          pattern 3: put n in the center of the permutation, put all odd numbers in the left of n (inside the permutation), put all even numbers in the right of n (inside the permutation).

          https://mirror.codeforces.com/contest/2091/submission/312559632

          • »
            »
            »
            »
            »
            »
            13 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            Let a[i] be the index of value i in the permutation. Then value i will be in the correct spot of the cyclic shift (i-a[i]) % n to the right. Then, the problem requires that each value of (i-a[i]) % n be distinct. One easy way to do this is to have $$$(i-a[i])=-i \pmod{n}$$$, which means that $$$a[i]=2i \pmod{n}$$$ (which is your first pattern). Obviously, this only works in distinct values for odd $$$n$$$.

»
13 months ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

Praise the sun!

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How to implement prefix sum in memoization? Anyone help. 312470925

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In F while selecting second hold on same row, it is done

dp[i][j][f]= dp[i][j][f]+∑j+dy=j−d dp[i][y][1]−dp[i][j][1]

But why here dp[i][y][1]? shouldn't it be dp[i][y][0]? why we take another hold if we already took 2?

»
13 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Praise the sun!

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone tell me why we define a and b as:

a=gcd(a,b)⋅x, b=gcd(a,b)⋅y for some x and y Where does this come from

  • »
    »
    12 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    $$$a$$$ is some number. Also $$$b$$$ is some number

    The problem requires us to work with $$$\text{gcd}(a,b)$$$

    What does it mean for both $$$a$$$ and $$$b$$$ to have a number, say $$$g$$$, as their greatest common divisor? It must be that:

    • $$$a$$$ is a multiple of $$$g$$$
    • $$$b$$$ is also a multiple of $$$g$$$

    So this means that:

    • $$$a$$$ is $$$gx$$$
    • $$$b$$$ is $$$gy$$$

    where $$$g$$$ is $$$\text{gcd}(a, b)$$$

    As a side note, I don't know about you, but I personally found it more intuitive to start my thinking from these three facts

    1. $$$1 \leq a \lt b \leq n$$$
    2. $$$\text{F}(a, b) = \dfrac{\text{lcm}(a, b)}{\text{gcd}(a ,b)}$$$
    3. $$$\text{F}(a, b)$$$ must produce a prime number
»
13 months ago, hide # |
Rev. 5  
Vote: I like it +5 Vote: I do not like it

for problem E , the sum of n overall test cases isnt required to be bounded we can realize that for each number x it has number_of_distinct_primes(x) relations under it which inspires a dp solution where

dp[i] = dp[i — 1] + number_of_distinct_primes(i);

312619387

also another solution for problem G

let dp[i][j] denote the following

for a movement with same parity as k the minimum reachable cell such that its index equals j mod i

for a movement with different parity from k the maximum reachable cell such that its index equals j mod i

ans is the first time a movement with the same parity as K has s % movement is reachable

transitions are kind of hard to explain honestly but i'll leave a submission and if maybe i've time later i'll explain them

312626019

complexity o(k * k * log(k))

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    dp[i] = dp[i — 1] + number_of_distinct_primes(i);

    what is the reasoning behind this equation?

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      for the constraint to be correct the larger number must be the smaller number * prime

      for 2 numbers a , b the larger is equal to the upperbound n

      now the problem is for a number n how many numbers a (less than n) such that the constraint is satisfied u can basically subtract 1 prime from the prime factors and the other number becomes a , subtracting the same prime twice gives similar a so we only subtract distinct primes

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For problem E, I was able to pre-compute the answer of all n up to 1e7. However, the test case limit was up to 1e3, gives enough time to have a loop over all prime which is not required with pre-compute. So, even if the test cases are 1e7, it would have worked.

»
13 months ago, hide # |
Rev. 6  
Vote: I like it +5 Vote: I do not like it

\[^-^]/

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

4 line solution for F (except for template)

Using lazy segment tree

https://mirror.codeforces.com/contest/2091/submission/312526600

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For Problem E n=5 how is ans 4?

for b = 2, a = 1 is only solution such that F(a, b) = 2 for b = 3, a = 1 is only solution such that F(a, b) = 3 for b = 4, there is no solution for b = 5, a = 1 is only solution such that F(a, b) = 5 b from 6 is > 5 since it's > n

So ans should be 3?

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone tell me why Binary Search has been mentioned in the tag of Problem F? I get it uses Prefix Sum along with dp states but where have we used binary search ??

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

2091G - Gleb and Boating I solved it in O(k^2 log k). Let's consider the number m (=k-2*j), let's get a dp of size m, where in the index i there will be a maximum or minimum position (depending on where we are going at the moment: left or right) that can be reached and which has a remainder i modulo M. Thus, we will iterate through all q from k to m and update dp, if at some point we have dp[s%m] ==s, then m suits us. Let's call this algorithm for m as check(m) -> true/false. If we consider all the numbers k, k-2, k-4, ..., and for them check(k), check(k-2), check(k-4), ... then false will go first, then true. We'll do a bin search for them and find the answer. If everything is false, we'll output 1.

My solution: 319579708

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

in F in last fomrula

dp[i][j][f]=dp[i][j][f]+∑j+dxy=j−dxdp[i+1][y][0]

as given in qn. for vertically we can go one by one up only. then how we directly going above

Each subsequent hold is not lower than the previous one; At least one hold is used on each level (i.e., in every row of the rectangle);

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can someone explain me how did thy get k=pi−i (mod n) from (i+k) mod n=pi in question C. I mean how did they simplify (i+k) mod n ?????????????????????? and what goes where ?

.

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem D O(1):

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t; cin >> t;
    while(t--){
        int n,m,k; cin >> n >> m >> k;
        int spots;
        if(k%n == 0) spots = k / n;
        else spots = k / n + 1;
        int splits = m - spots;
        int section_length = m / (splits+1);
        cout << section_length << '\n';
    }

    return 0;
}
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D: O(1)


void solve() { int n,m,k; cin>>n>>m>>k; int x=(k-1+n)/n; // debug(x); int y=(m-x)+1; cout<<(m/y)<<endl; }
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My thinking process for problem E is slightly different than the editorial, but using the same fact anyway.

Case $$$a = 1$$$: $$$lcm(a, b) = b$$$ and $$$gcd(a, b) = 1$$$, thus if $$$b$$$ is prime, it's a valid pair;

Case $$$a \gt 1$$$ and $$$gcd(a, b) = 1$$$: $$$lcm(a, b) = ab$$$ and gcd(a, b) = 1. But $$$ab$$$ cannot possibly be prime. Thus, pairs of coprime numbers are invalid.

Case $$$a \gt 1$$$ and $$$gcd(a, b) = k \ne a$$$: This is a case where $$$a$$$ and $$$b$$$ are non-coprime, and $$$b$$$ is not a multiple of $$$a$$$. We prove by contradiction. Assume $$$\frac{lcm(a, b)}{k} = prime$$$, then $$$lcm(a, b) = \frac{a \cdot b}{k} = k \cdot prime$$$. Let $$$m = \frac{a}{k}$$$ and $$$n = \frac{b}{k}$$$, then $$$\frac{km \cdot kn}{k} = k \cdot prime$$$, and we have $$$m \cdot n = prime$$$. This is a contradiction. Thus, we proved that for a pair of non-coprime numbers with a gcd smaller than $$$a$$$, the statement is invalid.

In the end, we are left with only the case where $$$1 \lt a \lt b$$$ and $$$gcd(a, b) = a$$$. It's obvious to pick $$$b = a \cdot prime$$$.

For implementation, I first generated all the primes up to 1e7, and used binary search to search for the number of primes that can be used to obtain $$$b$$$ while not exceeding $$$n$$$. This is my submission.

»
3 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I used the following approach for Problem D:

Idea:

Please notice that if k benches have to be distributed across n rows, atleast one of them will have ceil(k/n) desks.

Now, let the ith row contain ceil(k/n) desks. Let x = ceil(k/n).

Notice that this row initially had m empty spots. Now our task is to choose the locations of desks in such a way that the length of longest bench is minimised. If we calculate this minimal length, l, this will be our answer, because all other rows will have <= ceil(k/n) desks.

Please notice that m - x spots will remain empty in the row.

Our goal is to use these empty spots (gaps) to create discontinuities.

So, if m - x gaps are there, a group of x continuous desks can be divided into m - x + 1 smaller parts of continuous desks.

Now the largest of them will have ceil (x / (m - x + 1)) desks, which is the answer to the problem.

Please notice that this formula can be transformed to floor as floor(m / (m - x + 1)), which has been used in the code below.

Code