#abc243d. D - Moves on Binary Tree

D - Moves on Binary Tree

Score : 400400 points

问题描述

存在一棵完全二叉树,拥有 21010012^{10^{100}}-1 个顶点,编号分别为 1,2,...,21010011,2,...,2^{10^{100}}-1
顶点 11 是根节点。对于每个 1i<21010011\leq i < 2^{10^{100}-1},顶点 ii 拥有两个子节点:左边为顶点 2i2i,右边为顶点 2i+12i+1

高桥从顶点 XX 出发,并执行由字符串 SS 表示的 NN 步操作。第 ii 步操作如下:

  • SS 的第 ii 个字符是 U,则前往当前所在顶点的父节点。
  • SS 的第 ii 个字符是 L,则前往当前所在顶点的左子节点。
  • SS 的第 ii 个字符是 R,则前往当前所在顶点的右子节点。

请计算高桥在执行完 NN 步操作后所在的顶点的编号。在给定的情况下,保证答案不超过 101810^{18}

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a perfect binary tree with 21010012^{10^{100}}-1 vertices, numbered 1,2,...,21010011,2,...,2^{10^{100}}-1.
Vertex 11 is the root. For each 1i<21010011\leq i < 2^{10^{100}-1}, Vertex ii has two children: Vertex 2i2i to the left and Vertex 2i+12i+1 to the right.

Takahashi starts at Vertex XX and performs NN moves, represented by a string SS. The ii-th move is as follows.

  • If the ii-th character of SS is U, go to the parent of the vertex he is on now.
  • If the ii-th character of SS is L, go to the left child of the vertex he is on now.
  • If the ii-th character of SS is R, go to the right child of the vertex he is on now.

Find the index of the vertex Takahashi will be on after NN moves. In the given cases, it is guaranteed that the answer is at most 101810^{18}.

Constraints

  • 1N1061 \leq N \leq 10^6
  • 1X10181 \leq X \leq 10^{18}
  • NN and XX are integers.
  • SS has a length of NN and consists of U, L, and R.
  • When Takahashi is at the root, he never attempts to go to the parent.
  • When Takahashi is at a leaf, he never attempts to go to a child.
  • The index of the vertex Takahashi is on after NN moves is at most 101810^{18}.

Input

Input is given from Standard Input in the following format:

NN XX

SS

Output

Print the answer.

Sample Input 1

3 2
URL

Sample Output 1

6

The perfect binary tree has the following structure.

Figure

In the three moves, Takahashi goes 21362 \to 1 \to 3 \to 6.

Sample Input 2

4 500000000000000000
RRUU

Sample Output 2

500000000000000000

During the process, Takahashi may be at a vertex whose index exceeds 101810^{18}.

Sample Input 3

30 123456789
LRULURLURLULULRURRLRULRRRUURRU

Sample Output 3

126419752371

update @ 2024/3/10 10:27:24