Aperiodic Appointments

시간 제한1초메모리 제한1024 MB

요약
어떤 위치에서 끝나는 접두사가 어떤 비어 있지 않은 문자열을 K번 반복한 접미사를 가지면 그 위치가 1이 되는 이진 문자열에서 1의 개수를 센다.
난이도

어려움10점 중 8점

유형
문자열, 문자열 매칭, 수학, 조합론
정답자
아직 제출이 없습니다

문제

Nick has always struggled with maintaining habits. The problem is that he just can't stop maintaining them. If Nick does something KK times in a row, he has to keep doing it forever.

Luckily, he has started visiting Dr Patternson, an expert in PBT (Pattern Breaking Therapy). The principle of PBT is simple: Nick will visit Dr Patternson every day, and if he has done the same thing KK times in a row on a specific visit, the doctor will charge him money. This will motivate Nick to not continue this habit.

PBT has worked out great for Nick, as he has now successfully quit all his habits. Except for one, the habit of visiting Dr Patternson. The frequent visits are starting to take a toll on Nick's economy, so your task is to calculate how many times he has to pay the doctor for the next NN days.

Formally, let s=s_1s_2s_3…s_Ns = s\_1s\_2s\_3\dots s\_N be a string consisting of zeroes and ones. A one means that Nick has to pay the doctor on the iith day. This string is generated one character at a time, in the following way:

  1. s_i=0s\_i = 0 if i≤Ki \leq K.
  2. If i>Ki > K, then s_i=1s\_i = 1 if the previous characters contains a pattern that repeats KK times. More specifically, let s′=s_1s_2…s_i−1s' = s\_1s\_2\dots s\_{i-1}. If there is a nonempty string tt such that the last ∣t∣⋅K|t|\cdot K characters of s′s' can be written as t+t+⋯+tt+t+\dots + t, then s_i=1s\_i = 1. Otherwise s_i=0s\_i = 0.

You are given the numbers NN and KK, and your task is to calculate the number of ones in the string ss.

The picture represents Sample 1. An angry face means that Nick had to pay on the corresponding day.

입력

The input consists of one line with the integers NN and KK (1≤N≤1091 \leq N \leq 10^9, 2≤K≤1092 \leq K \leq 10^9).

출력

Print one integer, the number of ones in the string ss.

예제2

  1. 예제 1

    입력
    7 2
    
    예상 출력
    3
    
  2. 예제 2

    입력
    99 5
    
    예상 출력
    19