Editorial for TLE '16 Contest 1 P3 - Joey and Chemistry
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
First, note that the order of carbons, hydrogens, and oxygens in  do not matter. Only the amount of each element is important. We can get the count of each element using string manipulation.
We can simplify the entire complete combustion chemical equation into a system of 3 equations with 4 unknowns. The first equation will represent carbons, the second equation will represent hydrogens, and the third equation will represent oxygens.
Note that there can be an infinite number of solutions, but we want a solution where , 
, 
, and 
 are all positive integers and are in lowest terms with each other. We can first assume that 
 is equal to 1, meaning that 
 is equal to 
. Next, we need to make sure that 
 is divisible by 2 (so 
 can have an integer solution). If 
 is divisible by 2, 
 is equal to 
. If 
 is not divisible by 2, multiply 
 and 
 by 2 and set 
 equal to 
. Finally, we can solve for 
 using the same principles that we used to solve for 
. Namely, if 
 cannot initially be an integer, multiply the other variables by 2 so that 
 can be an integer. If 
, 
, 
, or 
 are not positive integers, then it is impossible to balance the equation.
Time Complexity: 
Comments