## DMOPC '19 Contest 7 P1 - Hydrocarbons

View as PDF

Points: 7
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

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:

• commented on Jan. 20, 2021, 10:34 a.m.

Why is this graph theory, shouldn't it just be implementation.

• commented on May 7, 2020, 10:31 a.m.

How are there comments on an editorial when there isn't one?

• commented on May 7, 2020, 1:44 p.m.

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?).

• commented on May 7, 2020, 1:58 p.m.

What is an example of a case that would fail many solutions?

• commented on May 7, 2020, 3:26 p.m.

Input:

0 1 1 2

Expected Output:

invalid