Skip to content

Instantly share code, notes, and snippets.

@winsrewu
Created January 24, 2026 14:38
Show Gist options
  • Select an option

  • Save winsrewu/189896bd7cbb62451e8a450c8f28f6e3 to your computer and use it in GitHub Desktop.

Select an option

Save winsrewu/189896bd7cbb62451e8a450c8f28f6e3 to your computer and use it in GitHub Desktop.
A blog

Okay, welcome back. In this video, I'm going to go through the Bronze group contest of the U-Cycle, the first contest in 2026. I didn't perform well during the actual contest, so I'm going to review the problems and see if I can solve them all now.

For this video, I’m trying out speech-to-text and might use some AI to summarize it for my blog later. I want to keep the recording short, just to remember the experience. If a video is two hours long, who would watch it, right? It’s more convenient for me to just record my thoughts and let the software handle the transcription. I don't care too much about the post-processing.

I may post separate videos for the three contest questions. This is the first one: "Chip Exchange."

I glanced at the solution earlier and it seemed like there were no loops—just pure math. I thought it wouldn't be too difficult. During the actual contest, I didn't perform well. It was late, I had just finished school, and my brain wasn't working properly. Also, using Java was a problem; it takes too long to write compared to C++. There's a lot of overhead and cost of abstraction in Java.

Anyway, let's begin properly. I’ll create a new file. The problem is "Chip Exchange." I'll write the main function first and set up fast I/O in C++.

I need to include the necessary headers and synchronize with stdio for speed. I'll define a solve function to handle each test case. The input format: T (number of test cases), then for each test case, five numbers: A, B, CA, CB, and FA.

The numbers can be up to 10^9, so I need to use long long. I'll read T and then loop through each case, calling solve(A, B, CA, CB, FA).

Now, the logic. The problem: We start with A chips of type A and B chips of type B. We can receive X additional random chips (each could be A or B). After receiving them, we can repeatedly exchange CB chips of type B for CA chips of type A. We need to guarantee that we end up with at least FA chips of type A in the worst case (i.e., for the worst possible distribution of the X random chips). We need to find the minimum such X.

Initially, I thought about iterating over possible values of X, but that’s not feasible since X can be huge. There must be a formula.

Let me reason. After receiving X chips, let b be the total number of B chips we have (initial B plus some from the X random chips). The number of A chips we can get via exchange is floor(b / CB) * CA. The total A chips we end with is: initial A, plus some A from the X random chips, plus the exchanged amount.

The worst case for a given total chips total = A + B + X is when the number of B chips is such that we get the minimum final A count. We need to ensure that even this minimum is at least FA.

I realized the function for final A count in terms of b (the number of B chips after receiving X) is a step function. It decreases when CB > CA and increases when CB < CA. The worst case will be at one of the "steps" in this function.

After a lot of scribbling and confusion (and taking a break for dinner), I was still stuck. My code was getting messy with special cases for CB < CA, CB == CA, and CB > CA. I tried to compute the worst-case point by finding the intersection with a line, but it wasn't working. My submissions were getting Wrong Answer or Time Limit Exceeded.

I spent over two hours on this. It felt too long for a Bronze problem. I decided to look at the official solution.

The solution idea is clever: Instead of thinking about the minimum X to guarantee success, think about the maximum total chips (A + B + X) for which it's still possible to fail (i.e., we cannot reach FA A chips). Then the answer is that maximum failing total plus one, minus the initial total (A + B).

So, find the maximum NA (number of A chips) and NB (number of B chips) such that NA + NB is maximized but we still cannot achieve FA A chips. Then X = (NA + NB + 1) - (A + B).

When can we not achieve FA? Let NA and NB be the counts after receiving chips. The final A count is NA + floor(NB / CB) * CA. For this to be less than FA, we need NA + floor(NB / CB) * CA <= FA - 1.

To maximize NA + NB under this constraint, we should:

  1. Make NB as large as possible but still have floor(NB / CB) be minimal relative to NA. The worst is when NB ≡ (CB - 1) (mod CB), i.e., NB = k * CB + (CB - 1). This maximizes NB without increasing the exchange gain.
  2. Then, set NA as large as possible given the constraint: NA = FA - 1 - floor(NB / CB) * CA.

But we also have the initial A and B. We need to express NA and NB in terms of initial values plus possible additions. Actually, we are searching over all possible NA >= A and NB >= B that are reachable by adding some chips (i.e., NA - A and NB - B are non-negative integers).

However, the solution simplifies further. It finds the maximum I (a non-negative integer) such that: NA = FA - 1 - I * CA NB = (CB - 1) + I * CB and NA >= A, NB >= B. Then the maximum failing total is NA + NB. If no such I exists with both NA >= A and NB >= B, then we can fail with just the initial chips? Actually, we need to check the initial condition separately.

I coded this logic: For each test case, compute the answer as follows:

  1. If with initial chips we already have A + floor(B / CB) * CA >= FA, then answer is 0 (we already guarantee FA).
  2. Otherwise, find maximum I such that: NA = FA - 1 - I * CA >= A NB = (CB - 1) + I * CB >= B This I is bounded by (FA - 1 - A) / CA and also by non-negativity. We compute the minimum I that satisfies both lower bounds? Wait, we want to maximize NA + NB, which is (FA - 1 - I*CA) + ((CB - 1) + I*CB) = FA + CB - 2 + I*(CB - CA). So if CB > CA, we want the largest possible I. If CB < CA, we want the smallest possible I. If CB == CA, then I doesn't affect the sum. So we need to consider the range of valid I (where NA >= A and NB >= B) and pick the I that maximizes the sum given the sign of (CB - CA).

But the official solution was very short. Let me implement it step by step.

We compute I as follows:

  • Let minI be the smallest integer such that NB >= B, i.e., I >= ceil((B - (CB - 1)) / CB). But since NB = (CB - 1) + I*CB, we have I >= ceil((B - (CB - 1)) / CB).
  • Also, NA >= A implies I <= floor((FA - 1 - A) / CA). So valid I exists if minI <= maxI. If no valid I, then the worst case is just using initial chips? Actually, we need to check the case I = 0 separately? Let's think.

The maximum failing total is NA + NB for the I that maximizes the sum. If CB > CA, we take largest I (i.e., maxI). If CB < CA, we take smallest I (i.e., minI). If CB == CA, any I gives same sum, so we can take minI (or maxI). Then answer = max(0, (NA + NB + 1) - (A + B)).

But we must also consider the possibility that we cannot fail at all even with no additional chips? That is covered by the initial check.

I implemented this and tested with the samples. It passed the given examples. Finally, after all that struggle, the solution was much simpler mathematically. I spent way too long overcomplicating it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment