C - Triangular Relationship Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300300

問題文

整数 N,KN,K が与えられます。NN 以下の正の整数の組 (a,b,c)(a,b,c) であって、a+b,b+c,c+aa+b,b+c,c+a がすべて KK の倍数であるようなものの個数を求めてください。 ただし、a,b,ca,b,c の順番を入れ替えただけの組も異なるものとして数えます。また、a,b,ca,b,c の中に同じものがあっても構いません。

制約

  • 1N,K2×1051 \leq N,K \leq 2\times 10^5
  • N,KN,K は整数である

入力

入力は以下の形式で標準入力から与えられる。

NN KK

出力

NN 以下の正の整数の組 (a,b,c)(a,b,c) であって、a+b,b+c,c+aa+b,b+c,c+a がすべて KK の倍数であるようなものの個数を出力せよ。


入力例 1Copy

Copy
3 2

出力例 1Copy

Copy
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),(3,3,3)(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1),(3,3,3) が条件を満たします。


入力例 2Copy

Copy
5 3

出力例 2Copy

Copy
1

入力例 3Copy

Copy
31415 9265

出力例 3Copy

Copy
27

入力例 4Copy

Copy
35897 932

出力例 4Copy

Copy
114191

Score : 300300 points

Problem Statement

You are given integers NN and KK. Find the number of triples (a,b,c)(a,b,c) of positive integers not greater than NN such that a+b,b+ca+b,b+c and c+ac+a are all multiples of KK. The order of a,b,ca,b,c does matter, and some of them can be the same.

Constraints

  • 1N,K2×1051 \leq N,K \leq 2\times 10^5
  • NN and KK are integers.

Input

Input is given from Standard Input in the following format:

NN KK

Output

Print the number of triples (a,b,c)(a,b,c) of positive integers not greater than NN such that a+b,b+ca+b,b+c and c+ac+a are all multiples of KK.


Sample Input 1Copy

Copy
3 2

Sample Output 1Copy

Copy
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)(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)(3,3,3) satisfy the condition.


Sample Input 2Copy

Copy
5 3

Sample Output 2Copy

Copy
1

Sample Input 3Copy

Copy
31415 9265

Sample Output 3Copy

Copy
27

Sample Input 4Copy

Copy
35897 932

Sample Output 4Copy

Copy
114191


2025-04-23 (Wed)
01:12:23 +00:00