TLE '16 Contest 1 P3 - Joey and Chemistry

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 128M

Author:
Problem types

jlsajfj has been goofing off in his Science 10 class, so he has no idea what complete combustion is! Not wanting to disappoint his teacher, who thinks jlsajfj could do better in the class if he tried, jlsajfj has decided to bother you into doing his homework.

The homework sheet involves balancing hundreds of complete combustion chemical equations. In his homework, only three types of atoms (elements) are involved:

  • Carbon (represented by C)
  • Hydrogen (represented by H)
  • and Oxygen (represented by O)

The general form of such an equation looks like this:

\displaystyle aR + bO_2 \to cCO_2 + dH_2O

R can be any combination of carbon, hydrogen, or oxygen.

In order for the chemical equation to be balanced, the number of atoms of each element (C, H, and O) must be equal on both sides of the arrow, since atoms cannot be created or destroyed during the reaction. This can be done by setting a, b, c, and d to some positive integer value, signifying the number of copies of the substance. A subscript in front of an element signifies the amount of that element in the substance. In the input and output, a subscript will simply be an integer after an element. If there is no subscript in front of an element, there is only one of it.

For example, 2CH_3OH (2CH3OH in input/output format) contains 2 carbon atoms, 8 hydrogen atoms, and 2 oxygen atoms.

Can you help jlsajfj finish his homework?

Input Specification

The first and only line of input will contain a single string, R. It is guaranteed that R will not begin with a number and will only contain numbers, C, H, and O.

The total amount of each element in R will not be greater than 100\,000.

Output Specification

On a single line, output the balanced chemical equation in the form of aR + bO2 -> cCO2 + dH2O, where a, b, c, and d are in lowest terms and cause the equation to be balanced, and R is the exact copy of what was given in the input.

If it is not possible to balance the equation, simply output Impossible.

Sample Input 1

CH3CH2CH3

Sample Output 1

1CH3CH2CH3 + 5O2 -> 3CO2 + 4H2O

Sample Input 2

CH3OH

Sample Output 2

2CH3OH + 3O2 -> 2CO2 + 4H2O

Sample Input 3

H2O

Sample Output 3

Impossible

Comments


  • 0
    7kxb  commented on May 5, 2022, 5:32 p.m.

    can someone explain why this is a WA (check batch 1 ignore the other test cases) https://dmoj.ca/submission/4544318


  • 2
    jlsajfj  commented on Sept. 22, 2016, 12:41 p.m.

    This might be my favourite question yet


    • 1
      Paradox  commented on Sept. 22, 2016, 2:46 p.m.

      no


  • 1
    imaxblue  commented on Sept. 21, 2016, 11:24 p.m.

    can you use 0 of an element?


    • 0
      ZQFMGB12  commented on Sept. 21, 2016, 11:40 p.m.

      "a, b, c, and d to some positive integer value"


  • 0
    kobortor  commented on Sept. 21, 2016, 9:53 p.m.

    you say R cannot begin with a number yet you say 2CH3OH in the sample input


    • 0
      ZQFMGB12  commented on Sept. 21, 2016, 9:55 p.m.

      2CH3OH is not in any sample input.


  • 1
    OnlyPascalVNM  commented on Sept. 21, 2016, 7:10 p.m.

    A little wrong in this problem and I don't have enough time to solve problem P4, P5 and P6. So sad :(


  • 0
    OnlyPascalVNM  commented on Sept. 21, 2016, 6:38 p.m.

    On a single line, output the balanced chemical equation in the form of aR + bO2 -> cCO2 + dH2O, where aa, bb, cc, and dd are in lowest terms and cause the equation to be balanced, and RR is the exact copy of what was given in the input. For example, 2CH3OH2CH3OH (2CH3OH in input/output) contains 2 carbon atoms, 8 hydrogen atoms, and 2 oxygen atoms. If R is 2CH3OH then how to print output?

    1. 2CH3OH + 3O2 -> 2CO2 + 4H2O
    2. 12CH3OH + 3O2 -> 2CO2 + 4H2O ( aR + bO2 -> cCO2 + dH2O, a = 1, R = 2CH3OH ) 1 or 2 ???

    • 1
      ZQFMGB12  commented on Sept. 21, 2016, 6:41 p.m.

      It is guaranteed that R will not begin with a number.


  • 1
    OnlyPascalVNM  commented on Sept. 21, 2016, 5:56 p.m.

    Can R is CH3(CH2)3(OH)6 ???


    • 1
      ZQFMGB12  commented on Sept. 21, 2016, 5:57 p.m.

      No, there are no brackets in R.