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 ...
随机推荐
- 安卓中 使用html来使文字变色Html.fromHtml
在这里 我是用的html使文字的个别颜色变红 String textStr = " 本课程为<font color=\"#FF0000\">" + ...
- 带权二分图——KM算法hdu2255 poj3565
进阶指南的板子好像有点问题..交到hdu上会T 需要了解的一些概念: 交错树,顶标,修改量 #include<iostream> #include<stdio.h> #incl ...
- git 命令行(三)-删除文件
在Git中,删除也是一个修改操作,我们实战一下,有一个多余的文件:src/common/Util2.js 我们需要删除这个文件, 一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用 rm命令 ...
- EF Code First数据库映射规则及配置
EF Code First数据库映射规则主要包括以下方面: 1.表名及所有者映射 Data Annotation: 指定表名 1 using System.ComponentModel.DataAnn ...
- C语言实现 计算个人所得税务2种方法
#include <stdio.h> #include <stdlib.h> /* 基于C语言的个人所得税计税系统 问题描述: 我国现行的个人所得税计算方法如下: 级数 全月应 ...
- PAT甲级——A1085 Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- [原创]Java调用PageOffice给Word中的Table赋值
Word中的table操作需要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才可以通过数据区域控制和操作table.因此,要想使用table,则必须在w ...
- [COCI2019] Mobitel
题目 显然不小于\(n\)这个东西我们不是很好搞,考虑正难则反,求出有多少条路径小于\(n\),之后拿\(C_{n+m}^m\)一减就好了 于是状态为\(dp[i][j][k]\)表示到\((i,j) ...
- 1、Redis的使用
非关系型数据库分类: 分类 典型代表 典型应用场景 数据类型 优点 缺点 键值 (key-value) Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle B ...
- 2019-9-2-C#判断文件属于文本或二进制
title author date CreateTime categories C#判断文件属于文本或二进制 lindexi 2019-09-02 12:57:37 +0800 2018-2-13 1 ...