Hackerrank--Mixing proteins(Math)

题目链接

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 A,B,C,D(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:

    A   B   C   D
    _   _   _   _
A|  A   B   C   D
B|  B   A   D   C
C|  C   D   A   B
D|  D   C   B   A

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 109 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 N and K, N being the length of the protein ring and K the desired number of seconds. 
Second line contains a string of length N containing uppercase letters A,B, C or D only, describing the ring.

Output Format
Output a single line with a string of length N, describing the state of the ring after Kseconds.

Constraints
1≤N≤106 
1≤K≤109

Sample Input:

5 15
AAAAD

Sample Output:

DDDDA

Explanation
The complete sequence of mutations is:

AAADD
AADAD
ADDDD
DAAAD
DAADA
DADDD
DDAAA
ADAAD
DDADD
ADDAA
DADAA
DDDAD
AADDA
ADADA
DDDDA
首先从矩阵可以观察到,“突变”可以看做是两个值的异或。然后通过观察前几个k的突变公式,可以得到,当k%2==0时,就是 s[i] = s[i] ^ s[(i + k) % n];对于上述等式不成立的k,可以通过枚举k的二进制的每一位,可以转化为上述情况。如k=6,那么k的二进制是 110, 也就是先变成4s,然后再变成2s后。Accepted Code:
 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4
 5 typedef long long LL;
 6 #define rep(i, n) for (int i = (0); i < (n); i++)
 7
 8 string s;
 9 int n, k, ch[2][1000050];
10 int main(void) {
11     ios::sync_with_stdio(false);
12     while (cin >> n >> k) {
13         cin >> s;
14         rep (i, n) ch[0][i] = s[i] - ‘A‘;
15
16         int c = 0;
17         for (LL i = 1; i <= k; i <<= 1) if (i & k) {
18             c ^= 1;
19             rep (j, n) ch[c][j] = ch[c^1][j] ^ ch[c^1][(j + i) % n];
20         }
21         rep (i, n) s[i] = ch[c][i] + ‘A‘;
22         cout << s << endl;
23     }
24
25     return 0;
26 }
 
				
时间: 2024-08-08 22:10:12

Hackerrank--Mixing proteins(Math)的相关文章

房价预测(HackerRank)

从今天开始要多做一些关于机器学习方面的竞赛题目,题目来源主要是Hackerrank和Kaggle.链接如下 Hackerrank:https://www.hackerrank.com/ Kaggle:https://www.kaggle.com/ 在Hackerrank中提交源代码,这就使得很多库都需要自己写,限制比较多.而Kaggle只需要提交数据,所以随便怎么搞都行.现在来讲第一道题,房价预测,这是Andrew Ng课程里的比较经典的例子.题目描述如下 题目:https://www.hack

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

Hackerrank Connected Cell in a Grid

Problem Statement You are given a matrix with m rows and n columns of cells, each of which contains either 1or 0. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. The connected and filled

【HackerRank】 Chocolate Feast

Little Bob loves chocolates, and goes to the store with $N money in his pocket. The price of each chocolate is $C. The store offers a discount: for every M wrappers he gives the store, he'll get one chocolate for free. How many chocolates does Bob ge

【HackerRank】Encryption

One classic method for composing secret messages is called a square code.  The spaces are removed from the english text and the characters are written into a square (or rectangle). The width and height of the rectangle have the constraint, floor(sqrt

【HackerRank】 有洞的地图

给你一个n*n的地图.地图中的每个格子有一个值表示该地区的深度.我们称一个地图中的一个格子为空洞,当且仅当该格子不在地图边缘并且每个和它相邻的格子都具有比它更小的深度.两个格子称为相邻如果它们共有一条边. 你要找到地图中所有的空洞,并且用X描述. 输入格式 第一行包含一个整数n,表示地图的规模. 接下来n行中每行包含n个无空白的正数字.每个数字(1-9)表示对应区域的深度. 输出格式 输出n行,表示最终的地图结果.每个空洞要用字符X替换. 约束条件 1<=n<=100 1≤n≤100 题解:简

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum