All Tracks Math Combinatorics Inclusion-Exclusion Problem

Gadget fan
Tag(s):

Combinatorics, Easy-Medium, Math

Problem
Editorial
Analytics

iMagine yourself as a loyal fan of one popular company that creates all kind of gadgets. After all of these years "that company" has created $$n$$ different mobile phones, enumerated from $$1$$ to $$n$$ and powering devices, enumerated the same way. Due to the production policy, phones can be charged only by devices with phone's version not exceeding charger's version. Moreover, phone's and device's total version has to be equal or less than $$k$$. For example, if phone's version is $$a$$ and charger's is $$b$$, then the phone can be charged if $$a \le b$$ and $$a + b \le k$$.

You are so loyal, you have already bought every device — all $$n$$ phones and $$n$$ chargers! Moreover, you are fond of tea and taking photos with your cool mug, but with your effort phone discharges in a couple of hours, and you have to take a phone and a charger with you to the tea-shop and, of course, a pair of different ones.

Your task is to find the number of days, you can visit the tea-shop with a different pair of phone and a charger, which you haven't brought before, so that you can charge the phone, according to the rules mentioned above.

Input format

The first line of input contains one integer $$t$$ — number of test cases.

For each case, single line contains two integers $$n$$ and $$k$$ — number of mobile phones and versions' maximum sum, respectively.

Constraints

$$1 \le t \le 10000$$

$$ 1 \le n, k \le 2 \cdot 10^9 $$

Output format

For each case, output on a separate line a single integer — number of different pairs of a phone and a charger, satisfying the rules described in the statement.

SAMPLE INPUT
3
5 4
3 1
2 3
SAMPLE OUTPUT
4
0
2
Explanation

Let the first number in a pair be phone's version and second — charger's.

Then, for the first test case, there are four days, you can go to the tea-shop with a different pair of phone and a charger, which you haven't brought before, so that you can charge the phone, according to the rules mentioned in the statement:

  1. {2, 2}
  2. {1, 2}
  3. {1, 3}
  4. {1, 1}

Next, for the second test case, versions' maximum sum is 1, therefore no pair of a charger and a phone exists.

Finally, for the third test case, there are two days:

  1. {1, 2}
  2. {1, 1}
Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic, Kotlin

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

February Easy '17

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications