Bagel World

5

2 votes
Mathematics, Hard, Modular arithmetic, Number Theory
Problem

There are T restaurants in bagel-world. The ith restaurant serves ni distinct kind of bagels. Being a foodie you visit all the restaurants and in ith restaurant you order ki bagels. You have to tell the number of ways the order can be placed in the ith restaurant given that the count of any kind of bagel should always be less than P. Two orders are considered different if there is atleast one kind of bagel whose count in the order is different. Since the answer can be large you have to output answer modulo P .

Note: P is a prime.

Constraints

  • 1T105
  • 2P7654321
  • P  is a prime number
  • 1ni1012
  • 1kini×(P1)

Input Format

  • First line contains two space separated integers T  and P  respectively. T  lines follow.
  • ith line contains two space separated integer ni and ki respectively denoting the number of distinct kind of bagels available in ith restaurant and the number of bagels that you order.

Output Format

  • For each testcase output a single integer, the value of answer modulo P .
  • ith line should be the answer for the ith restaurant.
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For the 1st restaurant, there are 2  kinds of bagels and the order consists of 3  bagels. All the possible ways to make the order are: (0,3),(1,2),(2,1) and (3,0). So the answer is 4  mod 5  i.e.4

Note: (x,y) here represents that you order x  bagels of first kind and y  bagels of second kind.

For the 2nd restaurant 5 the  possible ways are: (0,4),(1,3),(2,2),(3,1) and (4,0). So the answer is 5  mod 5  i.e. 0

For the 3rd restaurant the 3  possible ways are: (2,4),(3,3),(4,2). So the answer is 3  mod 5  i.e. 3

Note: (0,6),(1,5),(5,1) and (6,0) are NOT valid orders as count of any kind of bagel is always less than P .

For the 4th restaurant there are 19 possible ways to order. So the answer is 19 mod 5  i.e. 4

Editor Image

?