Many CRT

AtCoder
IDagc063_d
Time2000ms
Memory256MB
Difficulty
You are given positive integers $N, a, b, c, d$. Determine whether there is a non-negative integer $x$ such that $x\equiv a+kb \pmod{c+kd}$ for every $k=0,1,\ldots,N-1$. If it exists, find the smallest such $x$ modulo $998244353$. ## Constraints * $2\leq N\leq 10^6$ * $1\leq a,b,c,d\leq 10^6$ ## Input The input is given from Standard Input in the following format: $N$ $a$ $b$ $c$ $d$ [samples]
Samples
Input #1
2 1 2 3 4
Output #1
10

$x=10$ is the smallest non-negative integer such that $x\equiv 1\pmod{3}$ and $x\equiv 3\pmod{7}$.
Input #2
2 1 1 10 10
Output #2
\-1

No non-negative integer $x$ satisfies $x\equiv 1\pmod{10}$ and $x\equiv 2\pmod{20}$.
Input #3
100 20 30 2 3
Output #3
0

$x= 0$ is the smallest non-negative integer satisfying the condition.
Input #4
9 12 34 56 78
Output #4
827501367

$x=15977769171609124$ is the smallest non-negative integer satisfying the condition.
API Response (JSON)
{
  "problem": {
    "name": "Many CRT",
    "description": {
      "content": "You are given positive integers $N, a, b, c, d$. Determine whether there is a non-negative integer $x$ such that $x\\equiv a+kb \\pmod{c+kd}$ for every $k=0,1,\\ldots,N-1$. If it exists, find the smalles",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc063_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given positive integers $N, a, b, c, d$.\nDetermine whether there is a non-negative integer $x$ such that $x\\equiv a+kb \\pmod{c+kd}$ for every $k=0,1,\\ldots,N-1$. If it exists, find the smalles...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments