-5

Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
After the cutting each ribbon piece should have length a, b or c.
After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ribbon pieces after the required cutting.
The first line contains four space-separated integers n, a, b and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide.
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

kinda simple code, but can't deal with 45 test. can't come up with test that disproves this code. not TL, wrong answer

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
     int n, a, b, c;
     cin >> n;
     cin >> a >> b >> c;
     if (a < b) swap(a, b);
     if (a < c) swap(a, c);
     if (b < c) swap(b, c);
     for (int i = 0; i <= n / a; i++)
     {
         for (int j = 0; j <= n / b; j++)
         {
             for (int k = 0; k <= n / c; k++)
             {
                 if (n - (i * a + j * b + k * c) == 0)
                 {
                     int answer=i+j+k;
                     cout << answer;
                     return 0;
                 }
             }
         }
    }
    cout << 0;
    return 0;
}
1
  • 1
    Well done putting the problem statement in a block quote. With content you did not create yourself, please state who did and where to find it. Commented Dec 9, 2024 at 9:41

2 Answers 2

5

Your solution is wrong and I will give you an example:

n = 101, a = 50, b = 49, c = 3

Your solution will break with i = 0, j = 2, k = 1.

But the best solution is i = 1, j = 0, k = 17.

And the right solution should check all the possible ways of cutting:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int n, a, b, c;
    cin >> n;
    cin >> a >> b >> c;
    if (a < b) swap(a, b);
    if (a < c) swap(a, c);
    if (b < c) swap(b, c);
    int answer = 0;
    for (int i = 0; i <= n / a; i++)
    {
        for (int j = 0; j <= n / b; j++)
        {
            int rem = n - a*i - j*b;
            if (rem % c == 0) 
            {
                answer = max(answer, i+j+rem/c);
            }
        }
    }
    cout << answer;
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that test make it a lot easier. found right code on the internet and thinked of bruteforcing comparsion of all possible tests.
1

Your solution is almost right but you give up too easily on the first answer found.

You are asked to obtain the maximum number of pieces of exact ribbon lengths a,b,c so you can't just accept the first plausible solution. You must enumerate them all (at least for those that could be a possible win).

You can tighten the execution time a lot by keeping track of the amount already cut off and avoid the innermost loop entirely by testing for the remaining length being an exact multiple of c instead. There may be some extra number theory tricks to make it go a lot faster for very large n and moderate a,b,c.

The original code tweaked to find the optimum solution is as follows:

#include <iostream>

using namespace std;
int main()
{
   int n, a, b, c, answer, best = -1;
   cin >> n;
    cin >> a >> b >> c;
   if (a < b) swap(a, b);
   if (a < c) swap(a, c);
   if (b < c) swap(b, c);
   for (int i = 0; i <= n / a; i++)
   {
       int nia = n - i * a;
       for (int j = 0; j <= nia / b; j++)
       {
          int niajb = nia - j * b;
          if (niajb % c == 0)
          {
               int k = niajb / c;
               answer = i + j + k;
               cout << i << ", " << j << ", " << k << "\tbest " << answer << "\n";
               if (answer > best) best = answer;
               break;
           }
       }
//       if ((i > 1) && (best > 0)) break;
   }
   cout << best;
   return 0;
}

I have left the diagnostic print in so all candidate solutions are displayed as they are found. The commented out line to cut short the search once a strong answer is found may not be safe. I'm sure a much better shortcut could be constructed to skip impossible outer loop combinations. I have no time to do that.

2 Comments

yeah, intuition just suggested my method as pretty fair one, thank you.
("the diagnostic print" prints best where I see current - I'd just use "\t: ".)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.