The third simulation game of the 12th Blue Bridge Cup-sequence length

1. Problem description:

For integers v and p, define the Pierce sequence as:
a[1] = v
a[i] = p% a[i-1]
For example, when v = 8, p = 21, the corresponding Pierce sequence is
a[1 ] = 8
a[2] = 5
a[3] = 1
After calculation, the value becomes 0, which is not in the scope of our consideration. Therefore, when v = 8, p = 21, the length of the Pierce sequence is 3. When p is constant, the length of Pierce sequence may be different for different values ​​of v. When p = 8, if 1<=v<p, the longest Pierce sequence appears when v=13, it is (13, 8, 5, 1), and the length is 4. When p=2021, the longest Pierce sequence appears at v=1160. How long is this sequence?

2. Thinking analysis:

I feel that the description of this topic is very problematic. Some descriptions should be modified to: When p = 21, if 1<=v<p, the longest Pierce sequence appears at v = 13, in fact, it is known that p, After v, the corresponding Pierce sequence can be obtained, so the process can be simulated

3. The code is as follows:

if __name__ == '__main__':
    v, p = 1160, 2021
    seq = [v]
    while seq[-1] != 0:
        seq.append(p % seq[-1])
    # 弹出最后一个为0的元素
    seq.pop()
    print(len(seq))

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115143412