Invariant Polynomials

Time limit1sMemory limit512 MB

Summary
Count the dimension of the space of real polynomials in x, y of degree at most d that are invariant under rotation by 2π/n.
Level

Medium7 of 10

Topics
Math, Combinatorics, Number theory, Implementation
Solved
No attempts yet

Problem

Consider a real polynomial P(x,y)P(x, y) in two variables. We call it invariant under the rotation by an angle α\alpha if

P(xcos⁡α−ysin⁡α,  xsin⁡α+ycos⁡α)=P(x,y)P(x\cos\alpha - y\sin\alpha,\; x\sin\alpha + y\cos\alpha) = P(x, y)

holds for all real xx and yy.

Consider the real vector space of all two-variable polynomials of degree at most dd that are invariant under the rotation by 2π/n2\pi/n. Compute the dimension MM of this vector space.

You may find the following remark useful: every polynomial of degree at most dd can be written uniquely as

P(x,y)=∑i,j≥0i+j≤daij xiyjP(x, y) = \sum_{\substack{i, j \ge 0 \\ i + j \le d}} a_{ij}\, x^i y^j

for some real coefficients aija_{ij}.

Input

A single line with two positive integers dd and nn separated by one space. Both are less than 10001000.

Output

Output a single integer MM, the dimension of the vector space described above.

Examples3

  1. Example 1

    Input
    1 1
    
    Expected output
    3
    
  2. Example 2

    Input
    2 2
    
    Expected output
    4
    
  3. Example 3

    Input
    2 4
    
    Expected output
    2