This page is still under construction.

Parts of this page are still being built. What you see may change.

Fermat Equation (Fermat)

Time limit0.5sMemory limit1024 MB

Summary
Given a prime p and exponent n, count triples (x,y,z) with entries in 0..p-1 such that x^n + y^n = z^n mod p.
Level

Medium7 of 10

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

Problem

Given a prime pp and a natural number n≥1n \ge 1, write a program that finds the number mm of triples (x,y,z)(x, y, z) of integers x,y,zx, y, z (0≤x,y,z≤p−10 \le x, y, z \le p-1) satisfying

xn+yn≡zn(modp).x^n + y^n \equiv z^n \pmod p.

Here a≡b(modp)a \equiv b \pmod p means that a−ba - b is divisible by pp.

Input

The first line of the input contains a prime pp (p<10000p < 10000). The second line contains a natural number nn (1≤n≤100001 \le n \le 10000).

Output

Print one line to standard output consisting of the single integer mm.

Hint

Note For the input data used in grading, the value of mm is less than 2312^{31}.

Examples2

  1. Example 1

    Input
    3
    5
    
    Expected output
    9
    
  2. Example 2

    Input
    19
    21
    
    Expected output
    487