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≤109Sample 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 ;
}
Hackerrank--Mixing proteins(Math)的更多相关文章
- 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 ...
- Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water
题目链接:B.New Theatre Square 题意: 你要把所有"." 都变成"*",你可以有两个选择,第一种就是一次铺一个方块(1*1),第二种就是同一 ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Chrome V8引擎系列随笔 (1):Math.Random()函数概览
先让大家来看一幅图,这幅图是V8引擎4.7版本和4.9版本Math.Random()函数的值的分布图,我可以这么理解 .从下图中,也许你会认为这是个二维码?其实这幅图告诉我们一个道理,第二张图的点的分 ...
- Math.random()
Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...
- Math.abs()方法 取绝对值
定义和用法 abs() 方法可返回数的绝对值. 语法 Math.abs(x) 参数 描述 x 必需.必须是一个数值. 返回值 x 的绝对值. 实例 在本例中,我将取得正数和负数的绝对值: <sc ...
- C标准头文件<math.h>
定义域错误可以理解为超出了函数的适用范围,如果发生了定义域错误,设errno为EDOM 如果结果不能表示为double值,则发生值域错误,如果结果上溢,则函数返回HUGE_VAL的值,设errno为E ...
- HDOJ 2393. Higher Math
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- .NET中DataTable的常用操作
一.目的 在各种.NET开发中,DataTable都是一个非常常见且重要的类型,在与数据打交道的过程中可以说是必不可少的对象. 它功能强大,属性与功能也是相当丰富,用好的话,使我们在处理数据时,减少很 ...
- [Ceoi2016|BZOJ4936] Match
哈希+分治+stack 题目: 给你一个由小写字母组成的字符串s,要你构造一个字典序最小的(认为左括号的字典序比右括号小)合法的括号 序列与这个字符串匹配,字符串和括号序列匹配定义为:首先长度必须相等 ...
- [记]Windows 系统下设置Nodejs NPM全局路径
Windows下的Nodejs npm路径是appdata,担心安装的node_modules越来越多,导致C盘满,所以参考别人的博文,将node_modules安装的默认目录修改一下. 参考Wind ...
- 数论整除——cf1059D
用map是卡着过去的..题解用vector+离散化后常数小了十倍.. 总之就是把所有模数给保存下来然后离散化,再去匹配一下即可,最后有个细节 自己的 #include<bits/stdc++.h ...
- webstorm中使用git管理服务器上的代码——入门级
一.首先要确保电脑已经成功安装好git了.(记住git的安装位置) 二.这里需要给webstorm配置一下:依次点击:file –> Settings –> Version Control ...
- spss-数据抽取-拆分与合并
spss-数据抽取-拆分与合并 数据抽取也成为数据拆分,是指保留.抽取原数据表中某些字段.记录的部分信息,形成一个新字段.新纪录.分为:字段拆分和随机抽样两种方法. 一:字段拆分 如何提取" ...
- java基础之Character类概述
Character 类 在对象中包装一个基本类型 char 的值 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然 构造方法 public Cha ...
- 2018-11-20-UWP-开发中,需要知道的1000个问题
title author date CreateTime categories UWP 开发中,需要知道的1000个问题 lindexi 2018-11-20 09:28:53 +0800 2018- ...
- nfs 原理详解
一.NFS介绍 1)什么是NFS 它的主要功能是通过网络让不同的机器系统之间可以彼此共享文件和目录.NFS服务器可以允许NFS客户端将远端NFS服务器端的共享目录挂载到本地的NFS客户端中.在本地的N ...
- 二分判定 覆盖问题 BZOJ 1052
//二分判定 覆盖问题 BZOJ 1052 // 首先确定一个最小矩阵包围所有点,则最优正方形的一个角一定与矩形一个角重合. // 然后枚举每个角,再解决子问题 #include <bits/s ...