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

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →