[Solution] Fibonacci Strings solution codeforces

Fibonacci Strings solution codeforces – In all schools in Buryatia, in the 11 class, everyone is told the theory of Fibonacci strings.

Table of Contents

Fibonacci Strings solution codeforces

“A block is a subsegment of a string where all the letters are the same and are bounded on the left and right by the ends of the string or by letters other than the letters in the block. A string is called a Fibonacci string if, when it is divided into blocks, their lengths in the order they appear in the string form the Fibonacci sequence (𝑓0=𝑓1=1f0=f1=1𝑓𝑖=𝑓𝑖2+𝑓𝑖1fi=fi−2+fi−1), starting from the zeroth member of this sequence. A string is called semi-Fibonacci if it possible to reorder its letters to get a Fibonacci string.”

Burenka decided to enter the Buryat State University, but at the entrance exam she was given a difficult task. She was given a string consisting of the letters of the Buryat alphabet (which contains exactly 𝑘k letters), and was asked if the given string is semi-Fibonacci. The string can be very long, so instead of the string, she was given the number of appearances of each letter (𝑐𝑖ci for the 𝑖i-th letter) in that string. Unfortunately, Burenka no longer remembers the theory of Fibonacci strings, so without your help she will not pass the exam.

Fibonacci Strings solution codeforces

The first line contains one integer 𝑡t (1𝑡1041≤t≤104) — the number of test cases. The following is a description of the input data sets.

The first line of each test case contains one integer 𝑘k (1𝑘1001≤k≤100) — the number of letters in the alphabet.

The second line of each test case contains 𝑘k integers 𝑐1,𝑐2,,𝑐𝑘c1,c2,…,ck (1𝑐𝑖1091≤ci≤109) — the number of occurrences of each letter in the string.

Output

For each test case print the string “YES” if the corresponding string is semi-Fibonacci, and “NO” if it is not.

You can print “YES” and “NO” in any case (for example, the strings “yEs“, “yes“, “Yes” will be recognized as a positive answer).

Example
input

Copy
6
1
1
2
1 1
2
1 2
3
3 1 3
2
7 5
6
26 8 3 4 13 34
output

Copy
YES
YES
NO
YES
NO
YES

Fibonacci Strings solution codeforces

In the first test case, a one-character string is semi-Fibonacci, being itself a Fibonacci string.

In the second test case, a string of two different characters is Fibonacci.

In the third test case, the string “abb” (let the first of the alphabet letter be a, the second letter b) is not a semi-Fibonacci string, since no permutation of its letters (“abb“, “bab“, and “bba“) is a Fibonacci string.

In the fourth test case, two permutations of the letters of the string “abaccac” (the first letter is a, the second letter is b, the third letter is c) are Fibonacci strings — “abaaccc” and “cbccaaa“.

For Solution

“Click Here”

Leave a Comment