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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;

class Program
{
    public static List<string> plist = new List<string>();
    static void Main(string[] args)
    {
        long[] nk = Console.ReadLine().Split().Select(long.Parse).ToArray();
        var n = nk[0];
        var k = nk[1];

        // 候補
        var src = new List<string>() { };
        for (int i = 1; i <= n * 2; i++)
        {
            if (i % k == 0)
            {
                src.Add(i.ToString());
            }
        }

        // 候補の数字を使って三つの順列を作る
        var combinations = Combination.Enumerate(src, 3, withRepetition: true);

        var ans = 0;

        foreach (var elem in combinations)
        {
            var b = (int.Parse(elem[2].ToString()) - int.Parse(elem[0].ToString()) - int.Parse(elem[1].ToString())) / (-2);
            var a = int.Parse(elem[0].ToString()) - b;
            var c = int.Parse(elem[1].ToString()) - b;

            if (a + b == int.Parse(elem[0].ToString()) && b + c == int.Parse(elem[1].ToString()) && c + a == int.Parse(elem[2].ToString()))
            {
                if (a > 0 && b > 0 && c > 0 && a <= n && b <= n && c <= n)
                {
                    ans++;
                }
            }
            
        }

        Console.WriteLine(ans);
    }

    public static class Combination
    {
        public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items, int k, bool withRepetition)
        {
            if (k == 1)
            {
                foreach (var item in items)
                    yield return new T[] { item };
                yield break;
            }
            foreach (var item in items)
            {
                var leftside = new T[] { item };

                // item よりも前のものを除く （順列と組み合わせの違い)
                // 重複を許さないので、unusedから item そのものも取り除く
                var unused = withRepetition ? items : items.SkipWhile(e => !e.Equals(item)).Skip(1).ToList();

                foreach (var rightside in Enumerate(unused, k - 1, withRepetition))
                {
                    yield return leftside.Concat(rightside).ToArray();
                }
            }
        }
    }
}