Subprime Fibonacci Sequence
Time limit2sMemory limit512 MB
Generate terms with the divide-out recurrence and find the shortest repeating pair of consecutive terms within the first n terms, then print the cycle.
- Level
Medium6 of 10
- Topics
- Simulation, Hash map, Number theory, Implementation
- Solved
- No attempts yet
Problem
The Subprime function of a positive integer is defined as follows.
- if is 1 or a prime.
- Otherwise, , where is the smallest prime dividing .
A Subprime Fibonacci sequence is defined by:
For example:
Unlike the standard Fibonacci sequence, which grows exponentially, a Subprime Fibonacci sequence usually eventually repeats.
Write a program that takes as input the initial values and and a number of terms to compute , and determines whether the sequence starting with and repeats in the first terms.
The sequence repeats if there are integers and with for which and .
The length of the repeating sequence is if there is no integer with such that and . That is, the sequence from to is the shortest repeating sequence.
Input
The first line of input contains a single decimal integer (), the number of data sets that follow. Each data set is processed identically and independently.
Each data set consists of a single line of input. It contains the data set number , followed by the maximum number () of terms to compute, followed by the initial values and in that order ().
Output
For each data set there are multiple lines of output.
If a repeating sequence is found, the first line of output contains the data set number , followed by the index where the sequence first repeated, followed by the length of the shortest repeating subsequence. The following lines of output contain the (length + 2) terms of the sequence from term to term , 20 terms to a line (except possibly for the last line).
If a repeating sequence is not found in the first terms, the first line of output contains the data set number , followed by the number of terms , followed by the digit 0. The following line contains only the value of the sequence at (the -th term).