Mixing proteins


Problem Statement :


Some scientists are working on protein recombination, and during their research, they have found a remarkable fact: there are 4 proteins in the protein ring that mutate after every second according to a fixed pattern. For simplicity, proteins are called  (you know, protein names can be very complicated). A protein mutates into another one depending on itself and the protein right after it. Scientists determined that the mutation table goes like this:


Here rows denote the protein at current position, while columns denote the protein at the next position. And the corresponding value in the table denotes the new protein that will emerge. So for example, if protein i is A, and protein i + 1 is B, protein i will change to B. All mutations take place simultaneously. The protein ring is seen as a circular list, so last protein of the list mutates depending on the first protein.

Using this data, they have written a small simulation software to get mutations second by second. The problem is that the protein rings can be very long (up to 1 million proteins in a single ring) and they want to know the state of the ring after upto  seconds. Thus their software takes too long to report the results. They ask you for your help.

Input Format

Input contains 2 lines.
First line has 2 integers  and ,  being the length of the protein ring and  the desired number of seconds.
Second line contains a string of length  containing uppercase letters ,,  or  only, describing the ring.

Output Format

Output a single line with a string of length , describing the state of the ring after  seconds.



Solution :



title-img


                            Solution in C :

In  C :






#include <stdio.h>
#include <stdlib.h>

int main() {
	int i, j, n, k, c, *prot, *protnew, p2;
	scanf("%d %d",&n,&k);
	prot = malloc(n * sizeof(int));
	protnew = malloc(n * sizeof(int));
	for (i=0; i<n; i++) {
		while (c=getchar()-'A') if (c>=0 && c<=3) break;
		prot[i] = c;
	}
	p2 = 1;
	while (p2<k) p2 <<= 1;
	while (p2 >>= 1) {
		if (p2 & k) {
			for (i=0; i<n; i++) protnew[i] = prot[i] ^ prot[(i+p2) % n];
			for (i=0; i<n; i++) prot[i] = protnew[i];
		}
	}
	for (i=0; i<n; i++) putchar('A' + prot[i]);
	putchar('\n');
	return 0;
}
                        


                        Solution in C++ :

In  C++  :






#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>

using namespace std;

#define NMAX 1111111

char ch;
int A[2][NMAX];
int N, K, i, b, p, c, step;

int main() {
//	freopen("x.txt", "r", stdin);
	scanf("%d %d", &N, &K);
	for (i = 0; i < N; i++) {
		scanf(" %c", &ch);
		A[0][i] = ch - 'A';
	}

	for (c = 0, b = 30; b >= 0; b--)
		if (K & (1 << b)) {
			p = c;
			c = 1 - c;
			step = (1 << b) % N;
			for (i = 0; i < N; i++)
				A[c][i] = A[p][i] ^ A[p][(i + step) % N];
		}

	for (i = 0; i < N; i++)
		printf("%c", A[c][i] + 'A');
	printf("\n");
	return 0;
}
                    


                        Solution in Java :

In  Java  :







import java.io.*;

public class Solution {

    public static void solve(Input in, PrintWriter out) throws IOException {
        int n = in.nextInt(), k = in.nextInt();
        int[] a = new int[n];
        char[] c = in.next().toCharArray();
        for (int i = 0; i < n; ++i) {
            a[i] = c[i] - 'A';
        }
        for (int i = 1; i <= k; i *= 2) {
            if ((k & i) != 0) {
                int[] b = new int[n];
                for (int j = 0; j < n; ++j) {
                    b[j] = a[j] ^ a[(j + i) % n];
                }
                a = b;
            }
        }
        for (int i = 0; i < n; ++i) {
            c[i] = (char) (a[i] + 'A');
        }
        out.println(c);
    }

    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
        out.close();
    }

    static class Input {
        BufferedReader in;
        StringBuilder sb = new StringBuilder();

        public Input(BufferedReader in) {
            this.in = in;
        }

        public Input(String s) {
            this.in = new BufferedReader(new StringReader(s));
        }

        public String next() throws IOException {
            sb.setLength(0);
            while (true) {
                int c = in.read();
                if (c == -1) {
                    return null;
                }
                if (" \n\r\t".indexOf(c) == -1) {
                    sb.append((char)c);
                    break;
                }
            }
            while (true) {
                int c = in.read();
                if (c == -1 || " \n\r\t".indexOf(c) != -1) {
                    break;
                }
                sb.append((char)c);
            }
            return sb.toString();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}
                    


                        Solution in Python : 
                            
In  Python3 :








# A = 0 (0b00)
# B = 1 (0b01)
# C = 2 (0b10)
# D = 3 (0b11)
# a[x + 2^m, i] = a[x, i] ^ a[x, i + 2^m]

import sys

c2i = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
i2c = ['A', 'B', 'C', 'D']

n, k = [int(x) for x in input().strip().split()]

s = [c2i[x] for x in input().strip()]
s = [s, [0] * len(s)]

a, b = 1, 0

while k > 0:
    # swap
    a ^= b
    b ^= a
    a ^= b
    
    # closest power of 2
    cp2 = 1
    for i in range(29, 0, -1):
        if k - (1 << i) >= 0:
            cp2 = 1 << i
            break
            
    k -= cp2
    
    for i in range(n):
        s[b][i] = s[a][i] ^ s[a][(i + cp2) % n]
        

for i in s[b]:
    sys.stdout.write(i2c[i])
    
sys.stdout.flush()
                    


View More Similar Problems

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →