Decomposition Reaction CodeChef Solution

Problem -Decomposition Reaction CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Decomposition Reaction CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll m=pow(10, 9)+7;

int main() {
// 	ios_base::sync_with_stdio(false);
// 	cin.tie(NULL);
	ll N, M;
	cin >> N >> M;
	ll Q[N];
	for(ll i = 0; i < N; i++)
	    cin >> Q[i];
	vector<pair<pair<ll, ll>, vector<pair<ll, ll>>>> v(M);
	for(auto &i : v){
	    cin >> i.first.first >> i.first.second;
	    for(ll j = 0; j < i.first.second; j++){
	        pair<ll, ll> p;
	        cin >> p.first >> p.second;
	        i.second.push_back({p.first, p.second});
	    }
	}

	for(ll i = 0; i < M; i++){
	   // cout << v[i].first.first << " " << v[i].first.second << endl;
	   // for(int j = 0; j < v[i].first.second; j++)
	   //     cout << v[i].second[j].first << " " << v[i].second[j].second << " ";
	   // cout << endl;
	   ll temp = Q[v[i].first.first - 1];
	   Q[v[i].first.first - 1] = 0;
	   for(ll j = 0; j < v[i].first.second; j++)
	       Q[v[i].second[j].second - 1] = ((v[i].second[j].first*temp)%m + Q[v[i].second[j].second - 1])%m;
	}
	for(ll i = 0; i < N; i++)
	    cout << Q[i] << "\n";
	return 0;
}

Decomposition Reaction CodeChef Solution in C++14

#include <iostream>
#include<bits/stdc++.h>
#define ll long long int 
using namespace std;
int mod=(int)(1e9+7);

void solve(){
    ll n,m ; cin >> n >> m ;
    vector<ll>vt1(n) ;
    for(ll i=0 ; i<n ; i++) cin >> vt1[i] ;
    for(ll i=0 ; i<m ; i++){
        ll dcomp , parts ;
        cin >> dcomp >> parts ;
        // vt1[dcomp] = 0 ;
        for(ll j=0 ; j<parts ; j++){
            ll a,b ; cin >> a >> b ;
            vt1[b-1] += (a*(vt1[dcomp-1])%mod) ;
            vt1[b-1] %= mod ;
        }
        vt1[dcomp-1] = 0 ;
    }
    for(ll i=0 ; i<n ; i++) cout << vt1[i]%mod << endl ;
}

int main() {
	// your code goes here
	solve() ;
	return 0;
}

Decomposition Reaction CodeChef Solution in PYTH 3

# cook your dish here

from sys import stdin, stdout
# Faster I/O ======================
# wr = lambda i : stdout.write(str(i))
pr = lambda i : stdout.write(f'{i}\n')
inp = lambda : stdin.readline().strip()
im = lambda : map(int, stdin.readline().strip().split()) # int map
M = 1_000_000_007
# =================================
def main():
    n, m = im()
    q = list(im())
    for i in range(m):
        c, x = im()
        c_zero = q[c - 1]
        q[c - 1] = 0
        wc = list(im())
        for w, c_i in zip(wc[::2], wc[1::2]):
            q[c_i - 1] = ((q[c_i - 1]) % M + (c_zero * w) % M) % M
    pr('\n'.join(list(map(str, q))))
# =================================
if __name__ == '__main__':
    main()

Decomposition Reaction CodeChef Solution in C

#include <stdio.h> 
#define lli long long int
#define mod 1000000007

int main() {
	// Read the number of test cases.

		// Read the input a, b
		lli n, m;
		scanf("%lld %lld", &n, &m);

		// Compute the ans.
		lli q[n];
		for(lli i=0; i<n; i++){
		    scanf("%lld", &q[i]);
		}
		for(lli i=0; i<m; i++){
		    lli c,x;
		    scanf("%lld %lld", &c, &x);
		    lli eq[2*x];
		    for(lli j=0; j<2*x; j+=2){
		        scanf("%lld %lld", &eq[j], &eq[j+1]);
		        q[eq[j+1]-1]=(q[eq[j+1]-1]%mod+(eq[j]%mod)*(q[c-1]%mod))%mod;
		    }
		    q[c-1]=0;
		}
		for(lli i=0; i<n; i++){
		    printf("%lld\n", q[i]%(1000000000+7));
		}
		printf("\n");


	return 0;
}

Decomposition Reaction CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer s = new StringTokenizer(b.readLine());
        StringBuilder sb = new StringBuilder();
        int mod = (int)(Math.pow(10,9)+7);
        int n = Integer.parseInt(s.nextToken());
        int m = Integer.parseInt(s.nextToken());
        long[] arr = new long[n];
        s = new StringTokenizer(b.readLine());
        for(int i = 0;i<n;i++){
            arr[i] = Long.parseLong(s.nextToken());
        }
        while(m-- > 0){
            s = new StringTokenizer(b.readLine());
            int c = Integer.parseInt(s.nextToken());
            int x = Integer.parseInt(s.nextToken());
            s = new StringTokenizer(b.readLine());
            long d = arr[(c-1)];
            arr[c-1] = 0;
            for(int i = 0;i<x;i++){
                int w = Integer.parseInt(s.nextToken());
                int ci = Integer.parseInt(s.nextToken());
                long a = ((w%mod)*(d%mod))%mod;
                arr[(ci-1)] = (arr[(ci-1)]+a)%mod;
            }
        }
        for(long i : arr){
            sb.append(i + " ");
        }
        System.out.println(sb);
	}
}

Decomposition Reaction CodeChef Solution in PYPY 3

n,m=list(map(int,input().split(' ')))
q=list(map(int,input().split(' ')))
for i in range(0,m):
    c,x=list(map(int,input().split(' ')))
    eq=list(map(int,input().split(' ')))
    for e in range(0,len(eq),2):
        w,c2=eq[e],eq[e+1]
        q[c2-1]=q[c2-1]+q[c-1]*w
    q[c-1]=0
for i in q:
    print(i%1000000007)

Decomposition Reaction CodeChef Solution in PYTH

MD = 1000000007
st = raw_input().split()
N = int(st[0])
M = int(st[1])
A = [0]
st = raw_input().split()
for x in st:
	A.append(int(x))
# endfor x
for i in range(M):
	st = raw_input().split()
	C = int(st[0])
	X = int(st[1])
	n = A[C]
	A[C] = 0
	p = 0
	st = raw_input().split()
	for k in range(X):
		W = int(st[p])*n%MD
		p += 1
		C = int(st[p])
		p += 1
		A[C] = (A[C]+W)%MD
	# endfor k
# endfor i
for k in range(1,N+1):
	print A[k]
# endfor k

Decomposition Reaction CodeChef Solution in C#

using System;

public class Test
{
	public static void Main()
	{
		// your code goes here
		const long _MOD_ = 1000000007;
		
		var num = Console.ReadLine().Split();
		long N = long.Parse(num[0]);    //  Number of compounds
		long M = long.Parse(num[1]);    //  Number of equations 
		
		var q = Console.ReadLine().Split();
		long[] Q = Array.ConvertAll(q, long.Parse); //  N elements are there
		
		while (M-- > 0)
		{
		    var info = Console.ReadLine().Split();
		    long C = long.Parse(info[0]);   //  Decomposing compound
		    long X = long.Parse(info[1]);   //  Number of compounds it will decompose to
		    
		    var p = Console.ReadLine().Split();
		    long[] P = Array.ConvertAll(p, long.Parse);
		    
		    long Y = 2 * X;     //  Number of elements in array P
		    
		    long tmp = Q[C - 1];
		    Q[C - 1] = 0;
		    
		    for (int i = 0; i < Y; i += 2) 
		        Q[P[i + 1] - 1] = ((Q[P[i + 1] - 1] % _MOD_) + ((P[i] * tmp) % _MOD_)) % _MOD_;
		}
		
		for (int i = 0; i < N; i++) Console.WriteLine(Q[i]);
	}
}

Decomposition Reaction CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	n, m := readTwoNums(reader)
	Q := readNNums(reader, n)
	E := make([][]int, 2*m)

	for i := 0; i < 2*m; i += 2 {
		E[i] = readNNums(reader, 2)
		x := E[i][1]
		E[i+1] = readNNums(reader, 2*x)
	}

	res := solve(n, m, Q, E)

	var buf bytes.Buffer

	for _, ans := range res {
		buf.WriteString(fmt.Sprintf("%d\n", ans))
	}

	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const MOD = 1000000007

func modAdd(a, b int) int {
	a += b
	if a >= MOD {
		a -= MOD
	}
	return a
}

func modMul(a, b int) int {
	r := int64(a) * int64(b)
	return int(r % MOD)
}

func solve(n int, m int, Q []int, E [][]int) []int {
	var edge_count int
	for i := 0; i < len(E); i += 2 {
		edge_count += E[i][1]
	}

	g := NewGraph(n, edge_count)
	for i := 0; i < len(E); i += 2 {
		c0 := E[i][0]
		c0--
		for j := 0; j < len(E[i+1]); j += 2 {
			w, c := E[i+1][j], E[i+1][j+1]
			c--
			g.AddEdge(c0, c, w)
		}
	}

	var ord []int

	var dfs func(u int)

	vis := make([]bool, n)

	dfs = func(u int) {
		vis[u] = true
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if !vis[v] {
				dfs(v)
			}
		}
		ord = append(ord, u)
	}

	R := make([]int, n)

	for u := 0; u < n; u++ {
		R[u] = Q[u]
		if !vis[u] {
			dfs(u)
		}
	}

	for i := n - 1; i >= 0; i-- {
		u := ord[i]
		if g.node[u] > 0 {
			for j := g.node[u]; j > 0; j = g.next[j] {
				v := g.to[j]
				w := g.cost[j]
				R[v] = modAdd(R[v], modMul(R[u], w))
			}
			R[u] = 0
		}
	}
	return R
}

type Graph struct {
	node []int
	next []int
	to   []int
	cost []int
	cur  int
}

func NewGraph(n int, e int) *Graph {
	node := make([]int, n)
	next := make([]int, e+1)
	to := make([]int, e+1)
	cost := make([]int, e+1)
	g := Graph{node, next, to, cost, 0}
	return &g
}

func (g *Graph) AddEdge(u, v, w int) {
	g.cur++
	g.next[g.cur] = g.node[u]
	g.node[u] = g.cur
	g.to[g.cur] = v
	g.cost[g.cur] = w
}
Decomposition Reaction CodeChef Solution Review:

In our experience, we suggest you solve this Decomposition Reaction CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Decomposition Reaction CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Decomposition Reaction CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *