Mail.Ru Cup 2018 Round 3 B. Divide Candies

Divide Candies

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Arkady and his friends love playing checkers on an n × n n \times n n×n field. The rows and the columns of the field are enumerated from 1 1 1 to n n n.

The friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell ( i , j ) (i, j) (i,j) will have exactly ( i 2 + j 2 ) (i^2 + j^2) (i2+j2) candies of unique type.

There are m m m friends who deserve the present. How many of these n × n n \times n n×n sets of candies can be split equally into m m m parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different.

Input

The only line contains two integers n n n and m m m ( 1 ≤ n ≤ 1 0 9 1 \le n \le 10^9 1n109, 1 ≤ m ≤ 1000 1 \le m \le 1000 1m1000) — the size of the field and the number of parts to split the sets into.

Output

Print a single integer — the number of sets that can be split equally.

Example

i n p u t \tt input input
3 3
o u t p u t \tt output output
1
i n p u t \tt input input
6 5
o u t p u t \tt output output
13
i n p u t \tt input input
1000000000 1
o u t p u t \tt output output
1000000000000000000

Note

In the first example, only the set for cell ( 3 , 3 ) (3, 3) (3,3) can be split equally ( 3 2 + 3 2 = 18 3^2 + 3^2 = 18 32+32=18, which is divisible by m = 3 m=3 m=3).

In the second example, the sets for the following cells can be divided equally:

  • ( 1 , 2 ) (1, 2) (1,2) and ( 2 , 1 ) (2, 1) (2,1), since 1 2 + 2 2 = 5 1^2 + 2^2 = 5 12+22=5, which is divisible by 5 5 5;
  • ( 1 , 3 ) (1, 3) (1,3) and ( 3 , 1 ) (3, 1) (3,1);
  • ( 2 , 4 ) (2, 4) (2,4) and ( 4 , 2 ) (4, 2) (4,2);
  • ( 2 , 6 ) (2, 6) (2,6) and ( 6 , 2 ) (6, 2) (6,2);
  • ( 3 , 4 ) (3, 4) (3,4) and ( 4 , 3 ) (4, 3) (4,3);
  • ( 3 , 6 ) (3, 6) (3,6) and ( 6 , 3 ) (6, 3) (6,3);
  • ( 5 , 5 ) (5, 5) (5,5).

In the third example, sets in all cells can be divided equally, since m = 1 m = 1 m=1.

Tutorial

根据题意可以将题目转换为,在 1 1 1 n n n 的范围内,有多少个点满足 ( i 2 + j 2 ) m o d    m = 0 (i ^ 2 + j ^ 2) \mod m = 0 (i2+j2)modm=0,由于 n n n 的范围为 1 ≤ n ≤ 1 0 9 1 \le n \le 10 ^ 9 1n109,哪怕是 O ( n ) \mathcal O(n) O(n) 的时间复杂度也会超时,所以我们需要借助公式 $ (i ^ 2 + j ^ 2) \mod m = ((i \mod m) ^ 2 + (j \mod m) ^ 2) \mod m$,由于对 m m m 取模后一定比 m m m 小,这样就将需要遍历的范围缩小成了 m m m 的范围,而 m m m 的范围是 1 ≤ m ≤ 1000 1 \le m \le 1000 1m1000,所以可以用 O ( m 2 ) \mathcal O(m ^ 2) O(m2)​ 的时间复杂度解决问题

对于任意两个数,如果他们取模后的值为 x x x y   ( x , y ∈ [ 1 , m ] ) y\ (x,y \in [1, m]) y (x,y[1,m]) ( x 2 + y 2 ) m o d    m = 0 (x ^ 2 + y ^ 2) \mod m = 0 (x2+y2)modm=0,则这两个数是满足题意的,而对于任意数 x x x x , x + m , x + 2 m , … , x + k m ( 1 ≤ k ≤ n − x m ) x,x + m, x + 2m, \dots, x + km(1 \le k \le \frac{n - x}{m}) x,x+m,x+2m,,x+km(1kmnx) 均符合题意,所以对于一个数 x x x 共有 n − x m + 1 \frac{n - x}{m} + 1 mnx+1 个数取模等于 x x x,最后根据满足题意的两个数进行排列组合相乘即可

此解法时间复杂度为 O ( m 2 ) \mathcal O(m ^ 2) O(m2)

Solution

import sys
input = lambda: sys.stdin.readline().strip()

n, m = map(int, input().split())
ans = 0
for i in range(1, m + 1):
    cnt_i = (n + m - i) // m
    for j in range(1, m + 1):
        cnt_j = (n + m - j) // m
        if (pow(i, 2, m) + pow(j, 2, m)) % m == 0:
            ans += cnt_i * cnt_j
print(ans)
  • 24
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值