Tap here to switch tabs
Problem
Submissions

Problem: Magical String

med
30 min
Try to solve the Magical String problem.

Statement

A magical string s consists only of the numerical characters ′1′'1' and ′2′'2' and satisfies a unique self-generating property: grouping consecutive identical characters produces a sequence of group lengths that equals the string s itself.

The first few digits of the magical string s begin as follows:
s = "1221121221221121122...""1221121221221121122..."

Grouping the consecutive identical digits in s gives:
"1""1", "22""22", "11""11", "2""2", "1""1", "22""22", "1""1", "22""22", "11""11", "2""2", "11""11", "22""22", ......

The lengths of these consecutive groups are:
11, 22, 22, 11, 11, 22, 11, 22, 22, 11, 22, 22, ......

Notice that the sequence formed by the group lengths is identical to the magical string s itself.

Given an integer n, return the number of times the character ′1′'1' appears in the first n characters of the magical string s.

Constraints:

  • 1≤‘n‘≤1051 \leq `n` \leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Magical String

med
30 min
Try to solve the Magical String problem.

Statement

A magical string s consists only of the numerical characters ′1′'1' and ′2′'2' and satisfies a unique self-generating property: grouping consecutive identical characters produces a sequence of group lengths that equals the string s itself.

The first few digits of the magical string s begin as follows:
s = "1221121221221121122...""1221121221221121122..."

Grouping the consecutive identical digits in s gives:
"1""1", "22""22", "11""11", "2""2", "1""1", "22""22", "1""1", "22""22", "11""11", "2""2", "11""11", "22""22", ......

The lengths of these consecutive groups are:
11, 22, 22, 11, 11, 22, 11, 22, 22, 11, 22, 22, ......

Notice that the sequence formed by the group lengths is identical to the magical string s itself.

Given an integer n, return the number of times the character ′1′'1' appears in the first n characters of the magical string s.

Constraints:

  • 1≤‘n‘≤1051 \leq `n` \leq 10^5