Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:
Red boxes, each containing R red balls
Green boxes, each containing G green balls
Blue boxes, each containing B blue balls
Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes. How many triples of non-negative integers (r,g,b) achieve this?
Constraints
All values in
input are integers.
1 \leq R,G,B,N \leq 3000
Input
Input is given from Standard Input in the following format:
R G B N
Output
Print the answer.
Examples
Input
1 2 3 4
Output
4
Input
13 1 4 3000
Output
87058
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve a combinatorial counting problem: given positive integers R, G, B, N (each between 1 and 3000), count the number of triples of non-negative integers (r,g,b) such that:
r * R + g * G + b * B = N.
We need to output the count.
Constraints up to 3000, so we could do O(N^2 / something) but better to be O(N^2) at most. Since N up to 3000, O(N^2) is about 9 million, which is fine. But we need to count triples nonnegative.
We can think of iterating over r and g, compute remaining = N - rR - gG, and check if remaining >=0 and divisible by B. But r can go up to floor(N/R). With N=3000, R could be 1, so r up to 3000. Similarly g up to 3000. That's worst-case 9e6 iterations, which is acceptable in Python? 9 million is borderline but probably fine if optimized (should be < 0.1 sec? Actually Python can handle ~50 million simple operations per second? Not that high. 9 million is fine. But we need to be careful about loops nested and modulo operations. But 3000*3000 = 9 million, each step a subtraction and modulo, that's fine in Python.
But we can also do DP: dp[i] = number of ways to get sum i using any numbers of boxes (unbounded knapsack). That would be O(N * 3) ~ 9000, but careful: unbounded knapsack for three items. Actually we can do DP for two items and then count third. But DP would be O(N) using combinatorics? But we need distinct triples ordering matters? Since r,g,b are distinct types, order matters because they represent counts of different colors. So DP is straightforward: dp[0]=1; for each weight in [R,G,B], do unbounded knapsack: for i from weight to N: dp[i] += dp[i-weight]. But this counts combinations? However, unbounded knapsack for coin change problem counts number of ways to make sum using coins, where order of coins is considered? Wait, typical coin change problem: dp[i] = number of ways to make sum i using coins (unlimited supply) when order doesn't matter (combinations) if we iterate coins outer loop and then inner loop i from coin to N, we count combinations where order of coins doesn't matter. That's exactly what we need because r,g,b are not ordered? Actually (r,g,b) triple is ordered by type, but the combination approach counts each distinct combination of numbers of each coin exactly once. Indeed, when we iterate over coins one by one, we ensure that we consider all ways to use each coin type some number of times, without regard to sequence. That yields the number of ways to select nonnegative integers for each coin type. So DP works: dp[0]=1; for coin in [R,G,B]: for i from coin to N: dp[i] += dp[i-coin]; result = dp[N]. That's O(N) per coin, total O(3N) = 9000. That's trivial.
But caution: the DP counts combinations where we have unlimited coins. But is it correct? Let's test: For R=1,G=2,B=3,N=4, dp after R: dp[0]=1; then for i 1..4: dp[i] = dp[i-1], so dp[1]=1, dp[2]=1, dp[3]=1, dp[4]=1. After G: iterate i from 2 to 4: dp[2] += dp[0] => 1+1=2; dp[3] += dp[1] => 1+1=2; dp[4] += dp[2] => now dp[2] is 2, so dp[4] = 1+2=3. After B: i from 3 to 4: dp[3] += dp[0]? Wait dp[3] before B: 2; then dp[3] += dp[0] = 2+1=3; dp[4] += dp[1] => dp[4] before B: 3, plus dp[1]=1 => 4. So answer 4, matches example. Good.
So DP is simpler and faster. However, we must be careful: R, G, B up to 3000, and N up to 3000. DP array size 3001. That's trivial.
But one nuance: When using DP, the inner loop must be from coin to N inclusive. Since we are doing integer array, it's fine. Ensure dp is list of int of length N+1. Use Python.
Thus solution: read R,G,B,N. Initialize dp = [0]*(N+1); dp[0]=1. For coin in (R,G,B): for i in range(coin, N+1): dp[i] += dp[i-coin]. Print dp[N].
Edge cases: If coin > N, the loop does nothing, which is fine.
But does this DP count triples (r,g,b) correctly? Let's reason: We're counting number of ways to achieve sum N using unlimited coins of denominations R, G, B, where order of coins does not matter; each combination is defined by counts of each coin type. Indeed, typical coin change DP (with outer loop over coin types) counts each combination exactly once. So the triple (r,g,b) corresponds to using r coins of R, g of G, b of B. So dp[N] is the number of such triples. Yes.
Check with example 2: R=13, G=1, B=4, N=3000. We'll compute quickly? Not manually, but DP should work.
Given constraints, DP is O(3N) which is ~9000 operations, negligible.
Thus answer: implement.
However, note that there is a subtlety: dp[i] might be large? The number of triples could be large, but within Python's integer range (unbounded). But we need to output the answer as integer. For worst-case, say R=G=B=1, N=3000, then number of solutions is number of nonnegative integer solutions to r+g+b=3000 => C(3000+3-1,3-1)=C(3002,2) ~ 4.5 million, which is fine. Actually C(3002,2) ~ 4.5e6. But if R=G=B=1? All 1, then it's number of compositions? Wait r+g+b=3000, nonnegative solutions: (3000+3-1 choose 3-1)= (3002 choose 2)= ~4.5 million. That's within 32-bit integer? 4.5e6 fits in 32-bit. But if R,G,B are not all 1, maybe number of solutions could be larger? Let's bound: maximum number of triples occurs when coin denominations are small. The smallest possible denomination is 1. If we have two denominations of 1 and a third maybe >1, still number of solutions to r+g+... but the maximum occurs when all are 1? Actually if R=1, G=1, B=1, then it's partitions into 3 parts, count = C(N+3-1,3-1)=C(N+2,2)= ~4.5e6 for N=3000. If one of them is >1, the count is less. So maximum answer is ~4.5e6, which easily fits in Python int.
But wait, there is possibility that R=G=1, B=1? That's same. So answer won't exceed about 4.5e6, but we can just compute.
Thus DP is safe and efficient.
But let's check: DP with loops will count all ways. However, note that the DP recurrence dp[i] += dp[i-coin] for coin in [R,G,B] yields number of combinations. It works because we treat each coin type as separate. But caution: The order of loops matters; we must iterate over coin types in outer loop to avoid counting permutations. That's what we want. So it's correct.
Implementation:
def solve():
import sys
R,G,B,N = map(int, sys.stdin.read().split())
dp = [0]*(N+1)
dp[0] = 1
for coin in (R,G,B):
for i in range(coin, N+1):
dp[i] += dp[i-coin]
print(dp[N])
That's it.
But wait: There is a nuance: If a coin value is zero? But constraints say 1 <= R,G,B, so positive.
Thus done.
But the problem statement says "Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes." So (r,g,b) non-negative integers. So our DP counts exactly that.
Thus solution is simple.
However, the given example 2 yields 87058, and we can test with DP to ensure.
Thus final answer: DP.
But the user is ChatGPT, and the problem is from AtCoder (likely ABC 051 B? Actually it's from ABC 129? Not sure). But we are to output answer.
We'll provide explanation.
So final response: code and explanation.
But the user said "You are ChatGPT...", but we need to answer with solution. Usually we need to provide the solution in Python.
Thus final answer: code.
But wait, the problem might be originally from AtCoder "Boxes and Candies"? Not sure. But anyway.
One more thought: Could there be integer overflow in C++? Not in Python.
Thus we can output.
We'll produce final response with explanation and code.
But the instruction: "Print the answer." So we can output just code? As ChatGPT we might give explanation and code.
Better to give final answer: Python solution.
Thus answer:
code
def main():
import sys
R, G, B, N = map(int, sys.stdin.read().split())
dp = [0] * (N + 1)
dp[0] = 1
for coin in (R, G, B):
for i in range(coin, N + 1):
dp[i] += dp[i - coin]
print(dp[N])
if __name__ == "__main__":
main()