Mail.Ru Cup 2018 Round 3 B. Divide Candies

作者 : admin 本文共2521个字,预计阅读时间需要7分钟 发布时间: 2024-06-9 共4人阅读

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 imes 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 imes 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

t input

input

3 3

o

u

t

p

u

t

t output

output

1

i

n

p

u

t

t input

input

6 5

o

u

t

p

u

t

t output

output

13

i

n

p

u

t

t input

input

1000000000 1

o

u

t

p

u

t

t 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)
本站无任何商业行为
个人在线分享 » Mail.Ru Cup 2018 Round 3 B. Divide Candies
E-->