Problem:
Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

1 \leq N,K \leq 2\times 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191

Solution in JavaScript (node.js v5.12) (judge verdict: Accepted)

'use strict';

const main = (input) => {
    const nums = input.split(' ');
    const N = parseInt(nums[0], 10);
    const K = parseInt(nums[1], 10);
    // a+b が N の倍数で a+c が Nの倍数であるなら (b-c) は Nの倍数
    // 同様に (a-b), (a-c) も N の倍数
    let numBCCache1 = {};
    let numBCCache2 = {};
    let count = 0;
    for (let a = 1; a <= N; a++) {
        let m = a % K;
        let minBC = K - m;
        if (((minBC * 2) % K) == 0) {
            // bc が任意の組で満たす
            // bc がいくつあるか数える
            let numBC = numBCCache1[minBC];
            if (typeof numBC === 'undefined') {
                numBC = 0;
                for (let bc = minBC; bc <= N; bc += K) {
                    numBC++;
                }
                numBCCache1[minBC] = numBC;
            }
            count += numBC * numBC;
        } else {
            // bc が任意の組で満たさない
            let numBC = numBCCache2[minBC];
            if (typeof numBC === 'undefined') {
                numBC = 0;
                for (let bc = minBC; bc <= N; bc += K) {
                    if (((bc * 2) % K) == 0) {
                        numBC++;
                    }
                }
                numBCCache2[minBC] = numBC;
            }
            count += numBC;
        }
    }
    console.log(count);
};

const isTest = false;

if (isTest) {
    main('3 2');
    main('5 3');
    main('31415 9265');
    main('35897 932');
} else {
    main(require('fs').readFileSync('/dev/stdin', 'utf8'));
}