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 Go (1.6) (judge verdict: Time Limit Exceeded)

package main
import (
  "fmt"
  "os"
)
func main() {
  var n,k int
  var m int64
  fmt.Scan(&n,&k)
  if k == 1 {
    fmt.Println(int64(n)*int64(n)*int64(n))
    os.Exit(0)
  } else if k == 2 {
    o := (n+1)/2
    e := n-o
    m = int64(o)*int64(o)*int64(o)
    m += int64(e)*int64(e)*int64(e)
    fmt.Println(m)
    os.Exit(0)
  }
  m = 0
  var zmax int = (2*n)/k
  for z:=1;z<=zmax;z++ {
    if (z*k)%2 != 0 { continue }
    b := (z*k)/2
    var xmax int = (2*b)/k
    var ymax int = (b+n)/k
    for x:=ceil(b+1,k);x<=xmax;x++ {
      a := k*x-b
      for y:=ceil(2*b,k);y<=ymax;y++ {
        c := k*y-b
        if a != b && b != c && c != a {
          m += 6
        } else if a == b && b == c {
          m++
        } else {
          m += 3
        }
      }
    }
  }
  fmt.Println(m)
}
func ceil(n int,m int) int {
  return (n+m-1)/m
}