6853.Counting Necklaces

Time Limit: 1s Memory Limit: 512MB

Your task is to count the number of different necklaces that consist of $n$ pearls and each pearl has $m$ possible colors.

Two necklaces are considered to be different if it is not possible to rotate one of them so that they look the same.

Input Format(From the terminal/stdin)

The only input line has two numbers $n$ and $m$ : the number of pearls and colors.

  • $1 \le n,m \le 10^6$

Output Format(To the terminal/stdout)

Print one integer: the number of different necklaces modulo $10^9+7$ .

Sample Input

Copy
4 3
 · \n

Sample Output

Copy
24
  \n
Source: CSES, Mathematics, 2209

Submit

请先 登录

© 2025 FAQs