Prime Query

2.7

6 votes
Algorithms, Mathamatics, Basic Number Theory-1, Math, Number Theory
Problem

You are given an array A having N integers and Q queries. Each query is described by two integers L and R. For each query, find the number of pairs (i,j) such that Li<jR and Ai+Aj can be expressed as the sum of one or more prime numbers.

In the sum, you are allowed to use a prime number more than once.

Input format

  • The first line of input contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integers N.
  • The second line of each test case contains N space-separated integers A1,A2,,AN.
  • Then Q lines follow, each containing two space-separated integers L and R.

Output format

For each query, print the number of pairs that satisfies the given conditions in a separate line.

Constraints

1T102N1050Ai1051Q1051LRN

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • In the first query the pairs will be -
    • (3,5)=A3+A5=1+5=6 and 6 can be expressed as the sum of primes (2+2+2).
    • (4,5)=A4+A5=0+5=5 and 5 can be expressed as the sum of primes (2+3).

  • In the second query pairs will be (1,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,5),(4,5).

Editor Image

?