Bricking Up
Pharaoh Medjed is tired of all his hard work building pyramids. He wants to spice up the game by building some large squares for once. For this, Medjed wants to research how he can best obtain a perfect square amount of bricks to be stacked up into a massive square.
There are stonemasons, each with a talent score
. When stonemasons are working together, they can produce bricks equal to the product of all of their talent scores, as a team is worth more than the sum of its parts. Medjed wants to build
squares on different days, but at each day only some range
of the stonemasons are available. For each of these
ranges, Medjed wants to know how many contiguous subarrays of the available range can produce bricks sufficient for a perfect square number of bricks, i.e. how many subarrays of the given subarray have a perfect square product.
Input
The first line contains two integers (
), the number of stonemasons and the number of days.
The next line contains space-separated integers
(
), where each
represents the talent score of the
th stonemason.
The next lines each contain two integers
(
), the inclusive range of stonemasons that are available on the
th day.
Output
For each of the days, output how many arrangements of the available stonemasons can produce a perfect square amount of bricks, i.e. how many subarrays have a perfect square product of talent scores.
Example
Input 1
3 2
1 2 8
1 3
2 3
Output 1
3
1
The only subarrays with perfect square products are ,
,
. So the first day contains all of these, and the second contains only the third one.
Input 2
4 3
2 3 6 12
1 4
2 4
3 3
Output 2
1
0
0
The only counted subarray is with product
.
Input 3
5 3
4 9 2 18 8
1 5
3 5
1 2
Output 3
7
2
3
Comments