Decomposition Reaction solution codechef
A thermal decomposition reaction is breakdown of a compound into one or more compounds when heated.
For example,is a decomposition reaction. Here, unit of is converted into unit of and unit of after completion of the reaction.
You are givencompounds numbered from to . The initial amount of the compound is .
You are also givenequations. Every equation is of the form: .
- represents the compound which will decompose into compounds on heating.
- represents the compounds into which is decomposed.
- represents the number of units of produced due to the decomposition of .
- for all .
Note that a compound can decompose in at most one way only.
Find the final amount of each compound after prolonged heating. Since the number can be huge, print the answer modulo.
Input Format Decomposition Reaction solution codechef
- First line will contain two space separated integers and – the number of compounds and the number of equations.
- Second line will contain space separated integers – the initial amounts of each compound.
- The next lines contain the details of the equations. Each equation is represented using lines.
- First line of the equation contains two space separated integers , – the decomposing compound and the number of compounds in which it decomposes.
- Second line of the equation contains space separated integers – the number of units and the types of compounds into which decomposes.
Constraints Decomposition Reaction solution codechef
- , and .
- , and .
- Sum of over all test cases does not exceed .
Sample Input 1
4 2 5 10 12 2 1 2 3 2 4 3 3 1 2 4
Sample Output 1
0 25 0 66
Explanation Decomposition Reaction solution codechef
Test Cse: We have compounds initially. For better explanation, we refer the compounds and as and respectively. Initially, the amount of and present is and respectively.
The two equations are:
We know that
Considering only this reaction, the updated amounts of chemicals would be .
We also know thatunit of decomposes to units of . The total amount of available for decomposition would be units units were available initially and units were formed due to decomposition . Thus these units would produce units of .
Thus, after prolonged heating, the amounts of compounds are.