Veshy is struggling in chemistry so he asks you for help in naming hydrocarbons. As you know from chemistry, hydrocarbons are organic compounds consisting only of carbon and hydrogen atoms bonded covalently.
Covalent bonds in hydrocarbons can be either carbon-carbon single bonds (CC), carbon-carbon double bonds (CC), carbon-carbon triple bonds (CC), or carbon-hydrogen bonds (CH). Following the bonding rules, each carbon atom must have exactly four covalent bonds, and each hydrogen atom must have exactly one covalent bond for the molecule to be valid. As well, there must be one unique chain of bonds between any two atoms in the hydrocarbon; in other words, the structure must be acyclic.
Given carbon-carbon single bonds, carbon-carbon double bonds, carbon-carbon triple bonds, and carbon-hydrogen bonds, determine the formula of the hydrocarbon in terms of ( and are positive integers) if you can form a valid hydrocarbon (all carbon and hydrogen atoms have the correct number of bonds) using all the given bonds. If a valid hydrocarbon cannot be formed output invalid
.
Input Specification
The only line of input will contain four space-separated integers, .
Output Specification
There is only one line in the output.
If a valid hydrocarbon can be formed, output the chemical formula of the hydrocarbon in the form CnHm
, where and are the positive integers denoting the number of carbon and hydrogen atoms in the hydrocarbon respectively.
Sample Input 1
0 0 0 4
Sample Output 1
C1H4
Sample Input 2
1 0 0 3
Sample Output 2
invalid
Sample Input 3
1 0 0 6
Sample Output 3
C2H6
Explanation
In Sample 1, there are four carbon-hydrogen bonds which fills all required bonds:
In Sample 2, it is impossible to satisfy the number of covalent bonds required by the carbon atoms.
In Sample 3, each carbon atom is bonded to one other carbon atom and three other hydrogen atoms:
Comments
Why is this graph theory, shouldn't it just be implementation.
How are there comments on an editorial when there isn't one?
The test data for P1 was weak, which allowed many wrong solutions like the editorial solution to pass. We're working on a new editorial (and maybe some better test data?).
What is an example of a case that would fail many solutions?
Input:
Expected Output: