Geometric Progression

AtCoder
IDabc293_e
Time2000ms
Memory256MB
Difficulty
Given integers $A$, $X$, and $M$, find $\displaystyle \sum_{i = 0}^{X-1} A^i$, modulo $M$. ## Constraints * $1 \leq A, M \leq 10^9$ * $1 \leq X \leq 10^{12}$ * All values in the input are integers. ## Input The input is given from Standard Input in the following format: $A$ $X$ $M$ [samples]
Samples
Input #1
3 4 7
Output #1
5

$3^0 + 3^1 + 3^2 + 3^3 = 40$, which equals $5$ modulo $7$, so $5$ should be printed.
Input #2
8 10 9
Output #2
0
Input #3
1000000000 1000000000000 998244353
Output #3
919667211
API Response (JSON)
{
  "problem": {
    "name": "Geometric Progression",
    "description": {
      "content": "Given integers $A$, $X$, and $M$, find $\\displaystyle \\sum_{i = 0}^{X-1} A^i$, modulo $M$.",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc293_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given integers $A$, $X$, and $M$, find $\\displaystyle \\sum_{i = 0}^{X-1} A^i$, modulo $M$.\n\n## Constraints\n\n*   $1 \\leq A, M \\leq 10^9$\n*   $1 \\leq X \\leq 10^{12}$\n*   All values in the input are inte...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments