Bagel World
Practice
5 (2 votes)
Mathematics
Hard
Modular arithmetic
Number theory
Problem
84% Success 218 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

There are \(T\) restaurants in bagel-world. The \(i^{th}\) restaurant serves \(n_i\) distinct kind of bagels. Being a foodie you visit all the restaurants and in \(i^{th}\) restaurant you order \(k_i\) bagels. You have to tell the number of ways the order can be placed in the \(i^{th}\) 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

  • \(1 \le T \le 10^5\)
  • \(2 \le P \le 7654321\)
  • \(P\)  is a prime number
  • \(1 \le n_i \le 10^{12}\)
  • \(1 \le k_i \le n_i \times (P - 1)\)

Input Format

  • First line contains two space separated integers \(T\)  and \(P\)  respectively. \(T\)  lines follow.
  • \(i^{th}\) line contains two space separated integer \(n_i\) and \(k_i\) respectively denoting the number of distinct kind of bagels available in \(i^{th}\) restaurant and the number of bagels that you order.

Output Format

  • For each testcase output a single integer, the value of answer modulo \(P\) .
  • \(i^{th}\) line should be the answer for the \(i^{th}\) restaurant.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
Number TheoryAlgorithmsMathModulus Arithmetic
Points:50
1 votes
Tags:
Hard
Points:50
5 votes
Tags:
MathematicsHardModular arithmeticNumber Theory