#K11631. Count the 'a's in an Infinite Repeating String
Count the 'a's in an Infinite Repeating String
Count the 'a's in an Infinite Repeating String
You are given a string s
and an integer n
. Consider an infinite string formed by repeating s
indefinitely. Your task is to calculate the number of occurrences of the character a
in the first n
characters of this infinite string.
Formally, let T be the infinite string defined as:
\(T = s s s \cdots\)
You need to determine the number \(k\) such that \(k\) is the total number of occurrences of a
in the first n
characters of T.
Note: The value of n
can be extremely large, so your solution must be efficient.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains the string
s
. - The second line contains the integer
n
, representing the number of characters to consider.
outputFormat
Output a single integer to stdout representing the number of times the letter a
appears in the first n
characters of the infinite repeated string.
abcac
10
4