AND Palindromes

 You are given an array 

A consisting of N integers. Calculate the number of ways to divide this array into subsegments, such that the sequence formed by taking bitwise AND in each segment of the partition is a palindrome.

A partition is palindromic if Bi=Bk+1i for every 1ik. Your task is to calculate the number of palindromic partitions. Since this number can be large, calculate it modulo 998244353.

Input Format

  • The first line of input contains T — the number of test cases you need to solve.
  • The first line of each test case contains one integer N — the size of the array.
  • The second line of each test case contains N space-separated integers A1,,AN — the elements of the array A.

Output Format

For each test case, output on a new line the number of ways to partition A into subsegments, such that the bitwise AND of these segments form a palindrome.

Constraints

  • T1500
  • 2N3000
  • Sum of N over all test cases is at most 3000.
  • 0Ai<230

Sample Input 1 

3
3
1 1 1
4
3 4 5 6
5
1 1 0 1 0

Sample Output 1 

4
1
4

Explanation

Test case 1: The palindromic partitions are {[1,3]},{[1],[2,3]},{[1,2],[3]},{[1],[2],[3]}. To see why:

  • For {[1,3]}B=[A1A2A3]=[1]
  • For {[1],[2,3]}B=[A1,A2A3]=[1,1]
  • For {[1,2],[3]}B=[A1A2,A3]=[1,1]
  • For {[1],[2],[3]}B=[A1,A2,A3]=[1,1,1]

Clearly B is a palindrome in all 4 cases.

Test case 2: The only palindromic partition is {[1,4]}.

Test case 3: The palindromic partitions are {[1,3],[4,5]},{[1,3],[4],[5]},{[1,4],[5]},{[1,5]}.

Comments

Popular posts from this blog

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Flipkart Runway: Season 3 | 2025 | Internship

Python for Data Science NPTEL Assignment Solutions Week 4 2022

GATE 2022 Answer Key Release Date Announced; Details Here