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: Wrong Answer)

#include <cstdio>
#include <vector>

int N, K;
int ans = 0;

// 一方向に枝を伸ばす
/*
void search_pairs_incrsmall(int big, int small){
  if(big < small){
    return;
  }
  if((big + small) % K == 0){
    pairs.push_back(big);
    pairs.push_back(small);
  }
  search_pairs_incrsmall(big, small + 1);
  return;
}


void search_pairs(int big, int small){
  if(big < small){
    return;
  }
  if((big + small) % K == 0){
    pairs.push_back(big);
    pairs.push_back(small);
  }
  search_pairs(big-1, small);
  search_pairs_incrsmall(big, small+1);
  return;
}

void search_pairs_sameval(int val){
  if((2 * val) % K == 0){
    pairs.push_back(val);
    pairs.push_back(val);
  }
}
*/

int numcomb(int a, int b, int c)
{
  int num = 0;
  if(a==b) num++;
  if(a==c) num++;
  if(b==c) num++;
  switch (num) {
    case 0: return 6;
    case 1: return 3;
    case 3: return 1;
  }
}

void search_trios_A(int big, int mid, int sml)
{
  //printf("%d %d %d\n", big, mid, sml);
  
  if(big==mid){
    ans += ((mid - sml) / K) * 3;
    if((mid - sml) % K == 0){
      ans += 1;
    } else {
      ans += 3;
    }
  } else{
    ans += ((mid - sml) / K) * 6;
    if((mid - sml) % K == 0){
      ans += 3;
    } else {
      ans += 6;
    }
  }

  /*
  while(sml <= mid){
    ans += numcomb(big, mid, sml);
    sml += K;
  }
  */

  /*
  if(big < mid || mid < sml) return;
  if((big + mid) % K == 0 && (big + sml) % K == 0 && (mid + sml) % K == 0){
    //printf("%d %d %d\n", big, mid, sml);
    ans += numcomb(big, mid, sml);
    search_trios_A(big, mid, sml+K);
  } else {
    // probably not called
    search_trios_A(big, mid, sml+1);
  }
  */

}

void search_trios_B(int big, int mid, int sml)
{
  if(big < mid || mid < sml) return;
  if((big + mid) % K == 0 && (big + sml) % K == 0 && (mid + sml) % K == 0){
    //printf("%d %d %d\n", big, mid, sml);
    ans += numcomb(big, mid, sml);
    search_trios_B(big, mid+K, sml);
    search_trios_A(big, mid, sml+K);
  } else{
    //int res = (big + mid)%K;
    //int next_mid = mid + K - res;
    //search_trios_B(big, next_mid, sml);
    search_trios_B(big, mid + K, sml);
    //if((big + mid)%K==0){
    int res_withbig = (big + sml) % K;
    int res_withmid = (mid + sml) % K;
    //printf("(%d %d %d) res_withbig: %d, res_withmid: %d", big, mid, sml, res_withbig, res_withmid);
    //int leastcommon = res_withbig * res_withmid / gcd(res_withbig, res_withmid);
    if(res_withbig == res_withmid){
      search_trios_A(big, mid, sml + K - res_withbig);
    }
    //}
  }
}

void search_trios_C(int big, int mid, int sml)
{
  if(big < mid || mid < sml) return;
  if((big + mid) % K == 0 && (big + sml) % K == 0 && (mid + sml) % K == 0){
    //printf("%d %d %d\n", big, mid, sml);
    ans += numcomb(big, mid, sml);
    search_trios_C(big-1, mid, sml);
    search_trios_B(big, mid+K, sml);
    /*
    trios.push_back(big);
    trios.push_back(mid);
    trios.push_back(sml);
    */
  } else {
    if(K%2==0){
      search_trios_C(big-(K/2), mid, sml);
    } else {
      search_trios_C(big-K, mid, sml);
    }

    int res = (big + mid) % K;
    if(big%K == (mid + K - res)%K){
      search_trios_B(big, mid + K - res, sml);
    }
  }
}

int main(int argc, char const *argv[]) {
  scanf("%d %d", &N, &K);
  int big = N;
  if(2 * big % K != 0){
    if(K%2==0){
      big = N - N % (K / 2);
    }else{ // big % K == 0 にすればおk
      big = N - N % K;
    }
  }
  search_trios_C(big, 1, 1);
  /*
  if(N + 2 > K){
    int init_big = (N + 2) - (N + 2) % K;
    search_trios_C(init_big - 2, 1, 1);
  } else {
    if(K > N * 3){
      printf("0\n");
      return 0;
    } else {
      int init_mid = std::min(K-N-1, N);
      int init_sml = K-N-init_mid;
      search_trios_C(N, init_mid, init_sml);
    }
  }
  */
  //search_pairs(N, 1);
  /*
  for (int i = 1; i <= N; i++) {
    search_pairs_sameval(i);
  }
  for (int i = 0; i < pairs.size() / 2; i++) {
    printf("{%d, %d}\n", pairs[i*2], pairs[i*2 + 1]);
  }
  */
  /*
  for (int i = 0; i*3 < trios.size(); i++) {
    printf("{%d, %d, %d}\n", trios[i*3], trios[i*3 + 1], trios[i*3 + 2]);
  }
  */

  printf("%d\n", ans);

  return 0;
}