QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 1024 MB

#23938. 重复操作

統計

题目描述

给定正整数 $X$、$N$。

最初,黑板上写着整数 $X$。

JOI 君重复进行以下操作:

操作:设当前黑板上写着的数为 $x$。计算 $x$ 除以 $3$ 的余数,记作 $r$。根据 $r$ 的值,按如下方式改写黑板上写着的数:

  • 当 $r=0$ 时,将黑板上写着的数改写为 $x$ 加上 $1$ 所得的数。
  • 当 $r=1$ 时,将黑板上写着的数改写为 $x$ 乘以 $2$ 所得的数。
  • 当 $r=2$ 时,将黑板上写着的数改写为 $x$ 乘以 $3$ 所得的数。

请计算直到黑板上写着的数变为 $N$ 或更大为止所需的操作次数。

数据范围

  • $1 \le X < N \le 100\,000$。
  • 输入的所有值均为整数。

输入格式

输入按以下格式给出:

X
N

输出格式

请输出直到黑板上写着的数变为 $N$ 或更大为止所需的操作次数。

不要输出除答案以外的任何内容。(也不要输出提示输入的文字等。)

关于作答格式,也可以参考练习题及其解答示例。

样例 1

样例输入 1

2
40

样例输出 1

4
  • 最初,黑板上写着的数是 $2$。
  • 在第 $1$ 次操作中,操作开始时黑板上写着的数 $x$ 是 $2$。由于 $x$ 除以 $3$ 的余数 $r$ 是 $2$,因此将黑板上写着的数改写为 $x=2$ 乘以 $3$ 所得的数 $6$。
  • 在第 $2$ 次操作中,操作开始时黑板上写着的数 $x$ 是 $6$。由于 $x$ 除以 $3$ 的余数 $r$ 是 $0$,因此将黑板上写着的数改写为 $x=6$ 加上 $1$ 所得的数 $7$。
  • 在第 $3$ 次操作中,操作开始时黑板上写着的数 $x$ 是 $7$。由于 $x$ 除以 $3$ 的余数 $r$ 是 $1$,因此将黑板上写着的数改写为 $x=7$ 乘以 $2$ 所得的数 $14$。
  • 在第 $4$ 次操作中,操作开始时黑板上写着的数 $x$ 是 $14$。由于 $x$ 除以 $3$ 的余数 $r$ 是 $2$,因此将黑板上写着的数改写为 $x=14$ 乘以 $3$ 所得的数 $42$。
  • 由于进行 $4$ 次操作时黑板上写着的数首次变为 $40$ 或更大,因此输出 $4$。

样例 2

样例输入 2

3
4

样例输出 2

1

样例 3

样例输入 3

20
62

样例输出 3

3

样例 4

样例输入 4

1
100000

样例输出 4

19

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.