#K73607. Counting Unique Necklaces
Counting Unique Necklaces
Counting Unique Necklaces
You are given a set of beads in three colors: red, green, and blue. A necklace is created with a minimum of 3 beads, where the first bead is red and the last bead is blue, and the beads in between are green. Given the number of available red beads R, green beads G, blue beads B, and a maximum allowed length L for the necklace, compute the total number of unique necklaces that can be constructed.
The necklace must contain at least one green bead and the overall length must be between 3 and L, inclusive. In other words, if there are insufficient red or blue beads or if L is less than 3, no valid necklace can be formed.
The number of valid necklaces is determined by counting the permissible number of green beads (from 1 up to min(G, L-2)) that satisfy the total length constraint.
Formally, if we let \( g \) be the number of green beads used (with \(1 \le g \le \min(G, L-2)\)), then each valid configuration yields a necklace of length \( g+2 \) (including one red and one blue bead). Output the count of possible valid configurations.
inputFormat
The input is read from stdin and has the following format:
T R1 G1 B1 L1 R2 G2 B2 L2 ... RT GT BT LT
Here, the first line contains a single integer \(T\) denoting the number of test cases. Each of the following \(T\) lines contains four space-separated integers \(R\), \(G\), \(B\), and \(L\) that represent the number of red, green, blue beads available, and the maximum allowed length of the necklace, respectively.
outputFormat
For each test case, output a single integer on a new line to stdout representing the number of unique necklaces that can be formed under the given constraints.
## sample3
2 3 2 4
1 1 1 3
3 3 3 5
2
1
3
</p>