Decomposition Reaction solution codechef

Decomposition Reaction solution codechef

A thermal decomposition reaction is breakdown of a compound into one or more compounds when heated.

For example, PaQ+bRP→a⋅Q+b⋅R is a decomposition reaction. Here, 11 unit of PP is converted into aa unit of QQ and bb unit of RR after completion of the reaction.

You are given NN compounds numbered from 11 to NN. The initial amount of the ithith compound is QiQi.

You are also given MM equations. Every equation is of the form: C0W1C1+W2C2++WXCXC0→W1⋅C1+W2⋅C2+…+WX⋅CX.


  • C0C0 represents the compound which will decompose into XX compounds on heating.
  • CiCi (1iX)(1≤i≤X) represents the compounds into which C0C0 is decomposed.
  • WiWi (1iX)(1≤i≤X) represents the number of units of CiCi produced due to the decomposition of C0C0.
  • Ci>Ci1Ci>Ci−1 for all (1iX)(1≤i≤X).

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 109+7109+7.

Input Format Decomposition Reaction solution codechef

  • First line will contain two space separated integers NN and MM – the number of compounds and the number of equations.
  • Second line will contain NN space separated integers Q1,Q2,...,QNQ1,Q2,…,QN – the initial amounts of each compound.
  • The next 2M2⋅M lines contain the details of the MM equations. Each equation is represented using 22 lines.
  • First line of the ithith equation contains two space separated integers Ci,0Ci,0XiXi – the decomposing compound and the number of compounds in which it decomposes.
  • Second line of the ithith equation contains 2Xi2⋅Xi space separated integers Wi,1,Ci,1,Wi,2,Ci,2,,Wi,Xi,Ci,XiWi,1,Ci,1,Wi,2,Ci,2,…,Wi,Xi,Ci,Xi – the number of units and the types of compounds into which Ci,0Ci,0 decomposes.

Output Format

Print NN lines. The ithith line contains a single integer denoting the final amount of the ithith compound after prolonged heating modulo 109+7109+7.

Constraints Decomposition Reaction solution codechef

  • 0M<N1050≤M<N≤105
  • 0Qi<(109+7)0≤Qi<(109+7)
  • 1Ci,0<N1≤Ci,0<N
  • 1Xi(NCi,0)1≤Xi≤(N−Ci,0)
  • 1Wi,j<(109+7)1≤Wi,j<(109+7) (1jXi)(1≤j≤Xi) and (1iM)(1≤i≤M).
  • Ci,j<Ci,j+1Ci,j<Ci,j+1 (0j<Xi)(0≤j<Xi) and (1iM)(1≤i≤M).
  • Ci,0<Ci+1,0Ci,0<Ci+1,0 (1i<M)(1≤i<M)
  • Sum of XiXi over all test cases does not exceed 21052⋅105.

Sample Input 1 

4 2
5 10 12 2
1 2
3 2 4 3
3 1
2 4

Sample Output 1 


Explanation Decomposition Reaction solution codechef

Test Cse 11: We have 44 compounds initially. For better explanation, we refer the compounds 1,2,3,1,2,3, and 44 as A1,A2,A3,A1,A2,A3, and A4A4 respectively. Initially, the amount of A1,A2,A3,A1,A2,A3, and A4A4 present is 5,10,12,5,10,12, and 22 respectively.

The two equations are:

  • A13A2+4A3A1→3⋅A2+4⋅A3
  • A32A4A3→2⋅A4

We know that 11 unit of A1A1 produces 33 units of A2A2 and 44 units of A3A3. Thus, the initial 55 units of A1A1 decompose to 53=155⋅3=15 units of A2A2 and 54=205⋅4=20 units of A3A3. Note that now there are 00 units of A1A1 left.
Considering only this reaction, the updated amounts of chemicals would be [0,10+15,12+20,2]=[0,25,32,2][0,10+15,12+20,2]=[0,25,32,2].

We also know that 11 unit of A3A3 decomposes to 22 units of A4A4. The total amount of A3A3 available for decomposition would be 3232 units (12(12 units were available initially and 2020 units were formed due to decomposition)). Thus these 3232 units would produce 322=6432⋅2=64 units of A4A4.

Thus, after prolonged heating, the amounts of compounds are [0,25,0,64+2]=[0,25,0,66][0,25,0,64+2]=[0,25,0,66].

For Solution

Click Here

Leave a Comment