Rotated Palindromes

AtCoder
IDarc064_d
Time2000ms
Memory256MB
Difficulty
Takahashi and Aoki are going to together construct a sequence of integers. First, Takahashi will provide a sequence of integers $a$, satisfying all of the following conditions: * The length of $a$ is $N$. * Each element in $a$ is an integer between $1$ and $K$, inclusive. * $a$ is a _palindrome_, that is, reversing the order of elements in $a$ will result in the same sequence as the original. Then, Aoki will perform the following operation an arbitrary number of times: * Move the first element in $a$ to the end of $a$. How many sequences $a$ can be obtained after this procedure, modulo $10^9+7$? ## Constraints * $1≤N≤10^9$ * $1≤K≤10^9$ ## Input The input is given from Standard Input in the following format: $N$ $K$ [samples]
Samples
Input #1
4 2
Output #1
6

The following six sequences can be obtained:

*   $(1, 1, 1, 1)$
*   $(1, 1, 2, 2)$
*   $(1, 2, 2, 1)$
*   $(2, 2, 1, 1)$
*   $(2, 1, 1, 2)$
*   $(2, 2, 2, 2)$
Input #2
1 10
Output #2
10
Input #3
6 3
Output #3
75
Input #4
1000000000 1000000000
Output #4
875699961
API Response (JSON)
{
  "problem": {
    "name": "Rotated Palindromes",
    "description": {
      "content": "Takahashi and Aoki are going to together construct a sequence of integers. First, Takahashi will provide a sequence of integers $a$, satisfying all of the following conditions: *   The length of $a$ ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc064_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Takahashi and Aoki are going to together construct a sequence of integers.\nFirst, Takahashi will provide a sequence of integers $a$, satisfying all of the following conditions:\n\n*   The length of $a$ ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments