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 C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#include <limits>
#include <algorithm>
#include <map>

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;

using namespace std;

ll key(ll x, ll y, ll z, ll N) {
  return x * (N + 1) * (N + 1) + y * (N + 1) + z;
}

void calc(ll x, ll y, ll z, ll N, map<ll, bool> & memo, ll & res) {
  auto b  = x + y - z;
  if (b % 2 == 1) return;
  b /= 2;
  auto c = y - b;
  auto a = x - b;
  bool can = a <= N && b <= N && c <= N && a >= 1 && b >= 1 && c >= 1;
  if (!can) return;

  ll k = key(a, b, c, N);
  if (memo.find(k) != memo.end()) return;
  memo.insert(make_pair(k, true));
  if (can) {
//    std::cout << "(a, b, c)=("
//              << a << ", "
//              << b << ", "
//              << c << ")" << std::endl;
    res++;
    k = key(a, c, b, N);
    if (memo.find(k) == memo.end()) {
      res++;
      memo.insert(make_pair(k, true));
    }
    k = key(b, a,c, N);
    if (memo.find(k) == memo.end()) {
      res++;
      memo.insert(make_pair(k, true));
    }
    k = key(b, c, a, N);
    if (memo.find(k) == memo.end()) {
      res++;
      memo.insert(make_pair(k, true));
    }
    k = key(c, a, b, N);
    if (memo.find(k) == memo.end()) {
      res++;
      memo.insert(make_pair(k, true));
    }
    k = key(c, b, a, N);
    if (memo.find(k) == memo.end()) {
      res++;
      memo.insert(make_pair(k, true));
    }
  }
}

int main()
{
  ll N, K;
  cin >> N >> K;

  vector<ll> ks;
  map<ll, bool> memo;
  ll x;
  for (x = K; x <= 2 * N; x += K) {
    ks.push_back(x);
  }
  ll res = 0;
  for (auto i = 0; i < ks.size(); i++) {
    for (auto j = i; j < ks.size(); j++) {
      for (auto k = j; k < ks.size(); k++) {
        calc(ks[i], ks[j], ks[k], N, memo, res);
        calc(ks[i], ks[k], ks[j], N, memo, res);
        calc(ks[k], ks[i], ks[j], N, memo, res);
        calc(ks[k], ks[j], ks[i], N, memo, res);
        calc(ks[j], ks[i], ks[k], N, memo, res);
        calc(ks[j], ks[k], ks[i], N, memo, res);
      }
    }
  }


  cout << res << endl;
  return 0;
}