二つの組み合わせアルゴリズム問題

最近トップコーダーで二件の問いに答えようとしています。一つは狐が三角を染めるテーマでした。三つの色のボールの数と三角の列の数を知って正しい三角いくつ作れるかと言う問題です。「正しい」は同じ色のボールが隣ではないと言う事です。This problem can be solved by considering cases and carrying out algebra. The first case is that there is only one row possible. Then one of any color can be used, so the answer is the number of balls. If the number of rows mod 3 is 0 or 2, then an equal number of each color must be used in each triangle. The number of balls in a triangle of n rows is n(n+1)/2, so the number of balls of one color is n(n+1)/6. The number of balls of least common color, divided by this, yields the number of triangles. When the number of rows mod 3 is 1, there will be one more of one color than any other. The max number of triangles is the number derived above (i.e. the minimal-number color never provides the extra ball). The final answer is the min of this and the sum of all ball counts divided by 1+n(n+1)/6. This is based on the logic that the excess above the third allocation for any color is used to provide the extra ball.
(R+G+B)-(coPerTri*x)=x, where R is num red balls, G is num green balls, and B is num blue balls.
もう一つはコップケーキの配分です。三つの色のコップケーキの数が分かれば友達とコップケーキの割り当ての数を計算する目的です。