#P10510. 进制

    ID: 11831 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟洛谷原创O2优化进制洛谷月赛

进制

Background

To provide better differentiation, compared with the Lanqiao Cup national finals, all programming problems in this contest include additional samples. Please download them from the attachment.

Files with the suffix .in\bf{.in} are input files, and those with the suffix .out\bf{.out} are output files. You can use these additional samples to check the correctness of your program. However, we do not guarantee that passing the additional samples will necessarily give you the score you expect.

In addition, we provide more partial-score tiers than the Lanqiao Cup national finals to make the score distribution more scientific and reasonable.

Problem Description

Xiao Luo is learning ternary. He defines a ternary number as an infinitely long digit string aa where each digit is only one of 0,1,20,1,2.

Different from ordinary ternary numbers, Xiao Luo’s ternary numbers are written from left to right. For example, in the usual notation, the ternary representation of 44 is (0000 0011)3(0000\ 0011)_3, but in Xiao Luo’s notation it is (1100 0000)3(1100\ 0000 \cdots)_3.

Xiao Luo especially likes counting starting from 00, so he defines the 00-th digit of the ternary number to be the leftmost digit.

The table below shows, in Xiao Luo’s ternary notation, the lowest 88 digits of the number 3737 and their place values:

Ternary representation 11 00 11 00
Digit index 00 11 22 33 44 55 66 77
Place value 303^0 313^1 323^2 333^3 343^4 353^5 363^6 373^7

Now Xiao Luo has a positive decimal integer VV (obviously, it needs to be converted into Xiao Luo’s ternary representation), and there are three kinds of operations:

  • Operation 1: operate on the digit at index ii: 00 becomes 11, 11 becomes 22, 22 becomes 00.
  • Operation 2: operate on the digit at index ii: 00 becomes 22, 11 becomes 00, 22 becomes 11.
  • Operation 3: operate on the digit at index ii: 11 becomes 22, 22 becomes 11, 00 stays unchanged.

Xiao Luo will perform qq operations in total. After each operation, he needs to obtain the value represented by the ternary string. Please tell him the answer.

If anything is unclear, please refer to the sample explanation.

Input Format

The first line contains two positive integers V,qV,q.

The next qq lines each contain one operation in the form op i.

Note that you need to convert VV into a ternary string and use it as the initial ternary string.

Output Format

Output qq lines in total. The ii-th line should be the answer after the ii-th operation.

4 3
1 1
2 0
1 2
7
6
15

Hint

【Sample Explanation】

Initially, V=4V=4. After converting, Xiao Luo’s ternary number is 1100 0000\texttt{1100 0000} \cdots. Then 33 operations are performed:

  • Change the digit at index 11 from 11 to 22. The ternary number becomes 1200 0000\texttt{1200 0000} \cdots, which is 77 in decimal.
  • Change the digit at index 00 from 11 to 00. The ternary number becomes 0200 0000\texttt{0200 0000} \cdots, which is 66 in decimal.
  • Change the digit at index 22 from 00 to 11. The ternary number becomes 0210 0000\texttt{0210 0000} \cdots, which is 1515 in decimal.

【Constraints】

  • For 30%30\% of the testdata, V109V\leq 10^9 is guaranteed.
  • For another 30%30\% of the testdata, it is guaranteed that there is no operation 3.

For all testdata, 0V10180\leq V\leq 10^{18}, 1q1051\leq q\leq 10^5, and any obtained answer does not exceed 2×10182\times 10^{18}.

Translated by ChatGPT 5