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 Rust (1.42.0) (judge verdict: Accepted)

use std::collections::HashSet;
use std::io::*;
use std::str::FromStr;

//https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let N: usize = read();
    let K: usize = read();
    let mut num: Vec<u64> = vec![0; K];

    let mut ans = 0;
    for i in 1..=N {
        num[i % K] += 1; // num[x] = 1以上N以下に対して、Kで割ってx余る数が何個あるか
                         // 余りの範囲は0～(K-1)
    }
    //a,b,cを余りで考えて、一意に全探索する。
    //そして、その余りをとりうる個数（num[]）を掛けることで、組み合わせの個数を求める。
    for a in 0..K {
        let b = (K - a) % K;
        let c = (K - a) % K;
        if (b + c) % K != 0 {
            continue;
        }
        ans += num[a] * num[b] * num[c];
    }
    /* TLE全探索 O(N^3)
    for a in 1..=N{
        for b in 1..=N{
            for c in 1..=N{
                if (a+b)%K == 0
                && (b+c)%K == 0
                && (c+a)%K == 0{
                    ans += 1;
                }
            }
        }
    }
    */
    println!("{}", ans);
}