题目链接

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:
 #include <string>
#include <iostream>
using namespace std; typedef long long LL;
#define rep(i, n) for (int i = (0); i < (n); i++) string s;
int n, k, ch[][];
int main(void) {
ios::sync_with_stdio(false);
while (cin >> n >> k) {
cin >> s;
rep (i, n) ch[][i] = s[i] - 'A'; int c = ;
for (LL i = ; i <= k; i <<= ) if (i & k) {
c ^= ;
rep (j, n) ch[c][j] = ch[c^][j] ^ ch[c^][(j + i) % n];
}
rep (i, n) s[i] = ch[c][i] + 'A';
cout << s << endl;
} return ;
}
 
												

最新文章

  1. [转]PHP的执行流程,PHP扩展加载过程
  2. Tomcat启动服务报错:Unknown version string [3.1]. Default version will be used.
  3. linux中用shell获取昨天、明天或多天前的日期
  4. 工具类 dp转px 获取图片实际尺寸 获取屏幕尺寸
  5. HTTP.sys漏洞验证及防护
  6. SpringMVC学习总结(二)——DispatcherServlet详解
  7. python ciscolib模块
  8. edit distance leetcode
  9. HDU 1020 Encoding POJ 3438 Look and Say
  10. android学习---SeekBar和RatingBar
  11. list的基本操作实现
  12. Leetcode: The Maze III(Unsolved Lock Problem)
  13. STL 小白学习(9) 对组
  14. JS代码简单一段即可破解QQ空间删除说说
  15. jquery datatables+MVC+WCF
  16. Python 常用的正则表达式
  17. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
  18. 【读书笔记】iOS-如何选择本地化应用
  19. 1、Python-HelloWorld
  20. Linux的IO模型

热门文章

  1. godaddy账号以及域名被盗找回经历以及网络信息安全的思考
  2. DOM节点的创建、插入、删除
  3. Atcoder arc085
  4. Duilib入门文档提供下载
  5. Elasticsearch基本命令
  6. Origin使用自定义函数拟合曲线函数
  7. dp练习集
  8. 北京信息科技大学校赛 题解 | AK记录贴
  9. 解决windows8.1的依赖
  10. Mybatis编写配置文件时,需要注意配置节点的顺序