Count Of Feasts

3.5

2 votes
Algorithms, Counting and Arrangements, Dynamic Programming, Math
Problem

The king wants to eat a feast.

The food items will be placed in front of the king in a row, and the king eats the food items in the sequence (left to right) one by one.

He wants to eat different food items in groups of different sizes.

There are N food items and an array A consisting of N elements, where Ai denotes the size of the group in which the king wants to eat the ith food item.

Note:-

  •  The ith food item can only be served in multiples of Ai at once, and no other serving combinations are allowed.

Also, the king wants to eat food items in a particular range only.

There would be Q queries, each containing two integers Xi and Yi, denoting the minimum and maximum number of food items that the king wants to eat.

For each query, you need to calculate the total number of ways in which you can create a feast by arranging the food items in such a way that the king gets to eat the food items in groups of sizes desired by him, and the total food items eaten are between Xi and Yi (both inclusive).

The answer should be reported as modulo 109+7 as the number of feasts can be large.

Input Format:

  • The first line contains an integer N, the number of food items.
  • The second line contains N space-separated integers  denoting the array A.
  • The third line contains an integer Q, the number of queries.
  • The next Q lines contain two space-separated integers Xi and Yi, denoting the minimum and maximum number of food items the king wants to eat for that query, both inclusive.

Output Format:

  • For each query, output the total number of ways in which the king can eat a feast, satisfying the given conditions. Output the result modulo 109+7

Constraints:

1N1001Ai1051Q1051Xi,Yi105XiYi

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The different feasts possible are:-
(Here i denotes food item i)

size 1:- no possible feast
size 2:- {1,1}
size 3:- {2,2,2}
size 4:- {1,1,1,1}
size 5:- {3,3,3,3,3},{1,1,2,2,2},{2,2,2,1,1}
size 6:- {1,1,1,1,1,1},{2,2,2,2,2,2}

so, for the first query [1,4] :-
the total no of feasts that the king can eat if he wants to eat food items between 1 and 4 is equal to:-
feasts of size 1 +feasts of size 2 + feasts of size 3 + feasts of size 4 = 0+1+1+1 = 3;

so, for the second query [2,5] :-
the total no of feasts that the king can eat if he wants to eat food items between 2 and 5 is equal to:-
feasts of size 2 +feasts of size 3 + feasts of size 4 + feasts of size 5 = 1+1+1+3 = 6;
 

Editor Image

?