QOJ.ac

QOJ

Limite de temps : 30 s Limite de mémoire : 2048 MB Points totaux : 25

#12413. 平衡整数

Statistiques

由于 CCO 经常使用整数,Alice 需要学习有关整数的知识!一个正整数 $n$ 在 $b$ 进制下可以写成序列 $d_{m-1}d_{m-2}\dots d_1d_0$,如果满足以下条件:

  • 每个数字 $d_i$ 都在 $0$ 到 $b-1$ 之间(含边界)。
  • $d_{m-1} > 0$。
  • $n = d_{m-1} \times b^{m-1} + d_{m-2} \times b^{m-2} + \dots + d_1 \times b^1 + d_0 \times b^0$。

例如,整数 $2025$ 在 $19$ 进制下是序列 $(5, 11, 11)$,因为 $2025 = 5 \times 19^2 + 11 \times 19^1 + 11 \times 19^0$。

如果一个整数 $n$ 在 $b$ 进制下表示时,其数字的平均值为 $\frac{b-1}{2}$,则称该整数为 $b$-平衡数。 例如,$2025$ 是 $19$-平衡数,因为 $\frac{5 + 11 + 11}{3} = 9 = \frac{19-1}{2}$。

Alice 可以轻松找到 $19$-平衡数。然而,她在寻找在多种进制下都平衡的整数时遇到了困难。给定 $B$ 和 $N$,请帮助 Alice 找到最小的整数 $x$,使得:

  • 对于所有 $2 \le b \le B$,$x$ 都是 $b$-平衡数。
  • $x \ge N$。

输入格式

输入的第一行包含两个空格分隔的整数 $B$ 和 $N$ ($N \ge 1$)。

保证答案不超过 $10^{18}$。

下表显示了 25 分的分布情况:

分数 $B$ 的范围 $N$ 的范围
2 分 $2 \le B \le 7$ $1 \le N \le 10^4$
6 分 $2 \le B \le 6$ $N = 10^{10}$
2 分 $2 \le B \le 7$
9 分 $8 \le B \le 11$ $N = 1$
4 分 $B = 8$
2 分 $9 \le B \le 11$

输出格式

输出题目描述中要求的最小整数 $x$。

样例

样例输入 1

4 100

样例输出 1

141

说明 1

$141$ 在 $2$ 进制下是 $10001101$。数字平均值为 $\frac{1+0+0+0+1+1+0+1}{8} = 0.5 = \frac{2-1}{2}$。因此,$141$ 是 $2$-平衡数。

$141$ 在 $3$ 进制下是 $12020$。数字平均值为 $\frac{1+2+0+2+0}{5} = 1 = \frac{3-1}{2}$。因此,$141$ 是 $3$-平衡数。

$141$ 在 $4$ 进制下是 $2031$。数字平均值为 $\frac{2+0+3+1}{4} = 1.5 = \frac{4-1}{2}$。因此,$141$ 是 $4$-平衡数。

最后,$141 \ge 100$。

样例输入 2

7 10000000000

样例输出 2

16926961207710

说明

你可以随意使用这些代码片段作为你解决方案的一部分。

// Important: If x is 0, the result is undefined.
int base_2_length(unsigned long long x) {
return 64-__builtin_clzll(x);
}
int base_2_sum(unsigned long long x) {
return __builtin_popcountll(x);
}

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.