Prime in a binary string solution codechef

Prime in a binary string solution codechef


You are given a binary string SS of length NN. Your task is to check if there exists a substring of SS which is the binary representation of a prime number.

Formally, check if there exist integers LL and RR such that 1LRN1≤L≤R≤N, and the substring SLSL+1SL+2SRSLSL+1SL+2…SR, when treated as a binary integer, is prime.

Print "Yes" if such a substring exists, and "No" if it doesn’t.

Input Format

  • The first line of input contains a single integer TT, denoting the number of test cases. The description of TT test cases follows.
  • Each testcase consists of a single line of input, containing a binary string SS.

Output Format

For each test case, output a single line containing one string — "Yes" or "No", the answer to the problem.

You may print each character of the answer in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).


  • 1T2.51041≤T≤2.5⋅104
  • |S|105|S|≤105
  • The sum of |S||S| over all tests does not exceed 31053⋅105


Subtask 1 (30 points):

  • 1T1031≤T≤103
  • |S|10|S|≤10

Subtask 2 (70 points):

  • Original constraints

Sample Input 1 


Sample Output 1 



Test case 11: There is only one substring, namely "1", and it isn’t prime.

Test case 22: The substrings of the given string are {{"1""11""111""1""11""1"}}. Of these, "111" which represents 77 in binary, and "11" which represents 33 in binary, are primes.

Test case 33: One of the substrings of the string is "1101", which is the binary representation of 1313 — a prime.

Leave a Comment

Your email address will not be published.