Help Mike
Difficulty: Hard

Harvey Specter has agreed to take Mike Ross to a meeting filled with brilliant scientists at NSA Headquarters. But, as always, it's not going to be easy for Mike. He has to solve a puzzle given by Harvey. 

Harvey gives two numbers N and K and defines a set A.<br>

*A = { x : x is a [natural number](https://en.wikipedia.org/wiki/Natural_number) ≤ N }*  
(i.e), A = {1,2,3,4,5,6,...., N}   

Mike has to find the total number of pairs of elements A[i] and A[j] belonging to the given set, such that, i < j and their sum is divisible by K<br>

**Input Format**  
An integer T followed by T lines, each containing a pair of space separated integers N and K. 

**Output Format**  
T integers on separate lines. Each integer denotes the answer corresponding to that test case.

**Constraints**  
1<=T<=100  
K<=N<=10<sup>9</sup>    
1<=K<=10000  

**Sample Input**  

    2
    10 4
    7 3

**Sample Output**

    10
    7

**Explanation**  

For the 1<sup>st</sup> test case, there are 10 pairs whose sum is divisible by 4.   
(1,3), (1,7), (2,6), (2,10), (3,5), (3,9), (4,8), (5,7), (6,10) and (7,9)

For the 2<sup>nd</sup> test case, there are 7 pairs whose sum is divisible by 3.   
(1,2), (1,5), (2,4), (2,7), (3,6), (4,5) and (5,7)


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
#  1. INTEGER n
#  2. INTEGER k
#

def solve(n, k):
    # Write your code here



Editorial: Help Mike - Sept 2020 Editorial
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p><strong>Problem Statement</strong></p>

<p>Help Mike in finding the total number of pairs of elements A[i] and A[j] belonging to the given set such that i &lt; j and their sum is divisible by K</p>

<p><strong>Difficulty Level</strong> Easy-Medium</p>

<p><strong>Required knowledge</strong> : Basic Hashing , Maths</p>

<p><strong>Time complexity</strong> : O(K) , O(1) will be accepted . O(N/K) or O(N) will go for TLE(time limit exceeded) under given time constraints.</p>

<p><strong>Approach</strong> :</p>

<p>To solve the problem in O(K) time limit, we can create an array rem[K]. The array rem[i] will store the number of integers in the set A, such that on dividing by K, you get i as the remainder. rem[i] can be calculated in O(1) time.
rem[i] = (N-i)/K;</p>

<p>Now, the sum of two integers is divisible by K if:</p>

<ul>
<li>Both numbers are divisible by K and such selection can be made in rem[0](rem[0]-1)/2 ways.</li>
<li>On dividing first number by K, reminder is p then on dividing second number K, reminder must be K-p . i.e. number of ways can be rem[p]*rem[K-p] where p can belong to 0 to K/2.</li>
<li>K is even and on dividing both numbers, the reminder is K/2 i.e. rem[K/2]*(rem[K/2]-1)/2 .</li>
</ul>

<p>We can sum up all these possibilities to get the required output.</p>

<p><strong>Problem Setter's code:</strong></p>

<pre><code>#include&lt;bits/stdc++.h&gt;

using namespace std;

#define print(p) cout&lt;&lt;p&lt;&lt;endl;
int main()
{
    int T;
    long long int N,K,i;
    cin&gt;&gt;T;
    while(T--)
    {
        cin&gt;&gt;N&gt;&gt;K;
        long long int arr[K];
        arr[0] = N/K;
        for(i=1 ; i&lt; K ; i++)
            arr[i] = (int)(N-i)/K + 1;
        if(K%2!=0)
        {
            long long int sum = 0;
            sum  = sum + (arr[0]*(arr[0] - 1))/2;
            for(i=1 ; i&lt; (float)K/2 ; i++)
            {
                sum = sum + arr[i]*arr[K-i];
            }
            cout&lt;&lt;sum&lt;&lt;endl;
        }
        else
        {
            long long int sum = 0;
            sum  = sum + (arr[0]*(arr[0] - 1))/2;
            for(i=1 ; i&lt; K/2 ; i++)
            {
                sum = sum + arr[i]*arr[K-i];
            }
            sum = sum + (arr[K/2]*(arr[K/2] - 1))/2;
            cout&lt;&lt;sum&lt;&lt;endl;
        }
    }
    return 0;
}
</code></pre>

<p><strong>Problem Tester's Code</strong></p>

<pre><code>#!/usr/bin/py
import math

T = input()
1 &lt;= T &lt;= 100

for t in range(T):
    N, K = map(int, raw_input().strip().split(" "))
    assert 1 &lt;= N &lt;= 10**9
    assert 1 &lt;= K &lt;= 10**4
    assert K &lt;= N

    n_by_k = math.floor(N/K)
    n_mod_k = N%K
    count_less_than_k = K/2 if K % 2 == 1 else (K - 1)/2
    sum = count_less_than_k*n_by_k
    summation_limit = n_by_k - 1
    sum += K*(summation_limit*(summation_limit + 1))/2
    sum += (summation_limit+1)*(n_mod_k)
    if n_mod_k &gt; 0:
        extra = n_mod_k - count_less_than_k
        if K%2==0:
            extra -= 1
        sum += extra if extra &gt; 0 else 0

    print int(sum)
</code></pre>

Reference solution (C++):
###C++  
```cpp  
    
#include<bits/stdc++.h>
    
using namespace std;
    
#define print(p) cout<<p<<endl;
int main()
{
    int T;
    long long int N,K,i;
    cin>>T;
    while(T--)
    {
        cin>>N>>K;
        long long int arr[K];
        arr[0] = N/K;
        for(i=1 ; i< K ; i++)
            arr[i] = (int)(N-i)/K + 1;
        if(K%2!=0)
        {
            long long int sum = 0;
            sum  = sum + (arr[0]*(arr[0] - 1))/2;
            for(i=1 ; i< (float)K/2 ; i++)
            {
                sum = sum + arr[i]*arr[K-i];
            }
            cout<<sum<<endl;
        }
        else
        {
            long long int sum = 0;
            sum  = sum + (arr[0]*(arr[0] - 1))/2;
            for(i=1 ; i< K/2 ; i++)
            {
                sum = sum + arr[i]*arr[K-i];
            }
            sum = sum + (arr[K/2]*(arr[K/2] - 1))/2;
            cout<<sum<<endl;
        }
    }
    return 0;
}
```