QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#8441. 非周期性预约

Statistics

Nick 一直难以维持习惯。问题在于他一旦开始就停不下来。如果 Nick 连续 $K$ 次做某件事,他就必须永远做下去。

幸运的是,他开始拜访 Patternson 医生,一位 PBT(打破模式疗法)专家。PBT 的原则很简单:Nick 每天都会拜访 Patternson 医生,如果他在某次拜访时已经连续 $K$ 次做了同一件事,医生就会向他收费。这将激励 Nick 不再继续这个习惯。

PBT 对 Nick 非常有效,他现在已经成功戒掉了所有的习惯。除了一个,那就是拜访 Patternson 医生的习惯。频繁的拜访开始对 Nick 的经济造成负担,所以你的任务是计算他在接下来的 $N$ 天里需要付给医生多少次钱。

形式化地,令 $s = s_1s_2s_3 \dots s_N$ 为一个由 0 和 1 组成的字符串。1 表示 Nick 在第 $i$ 天需要付钱给医生。该字符串按以下方式逐个字符生成:

  1. 如果 $i \le K$,则 $s_i = 0$。
  2. 如果 $i > K$,则如果之前的字符包含一个重复了 $K$ 次的模式,则 $s_i = 1$。更具体地说,令 $s' = s_1s_2 \dots s_{i-1}$。如果存在一个非空字符串 $t$,使得 $s'$ 的最后 $|t| \cdot K$ 个字符可以写成 $t + t + \dots + t$(共 $K$ 个 $t$),则 $s_i = 1$。否则 $s_i = 0$。

给定数字 $N$ 和 $K$,你的任务是计算字符串 $s$ 中 1 的个数。

输入格式

输入包含一行,包含整数 $N$ 和 $K$ ($1 \le N \le 10^9, 2 \le K \le 10^9$)。

输出格式

输出一个整数,即字符串 $s$ 中 1 的个数。

样例

输入格式 1

7 2

输出格式 1

3

输入格式 2

99 5

输出格式 2

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.