Another Mod of Linear Problem

AtCoder
IDabc443_g
Time2000ms
Memory256MB
Difficulty
You are given integers $N,M,A,B$. Define an integer sequence $X=(X_0,X_1,\ldots,X_{N-1})$ as $\displaystyle X_k = (Ak+B) \bmod M$. Find the number of integers $k$ satisfying $0 \le k < N$ and $X_k > k$. You are given $T$ test cases; solve each of them. ## Constraints * $1\le T\le 3\times 10^5$ * $1\le N \le M\le 10^9$ * $0\le A,B < M$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $T$ $\text{case}_1$ $\text{case}_2$ $\vdots$ $\text{case}_T$ Each test case is given in the following format: $N$ $M$ $A$ $B$ [samples]
Samples
Input #1
4
4 6 4 3
7 7 3 1
10 46 0 12
443 2026 131 210
Output #1
2
3
10
395

Consider the first test case.

*   When $k=0$: $X_0=(4\times 0+3)\bmod 6=3$, so $X_k>k$ holds.
*   When $k=1$: $X_1=(4\times 1+3)\bmod 6=1$, so $X_k>k$ does not hold.
*   When $k=2$: $X_2=(4\times 2+3)\bmod 6=5$, so $X_k>k$ holds.
*   When $k=3$: $X_3=(4\times 3+3)\bmod 6=3$, so $X_k>k$ does not hold.

From the above, the integers $k$ satisfying $0 \le k < 4$ and $X_k>k$ are $k=0,2$, which is two integers. Thus, output $2$ on the first line.
API Response (JSON)
{
  "problem": {
    "name": "Another Mod of Linear Problem",
    "description": {
      "content": "You are given integers $N,M,A,B$. Define an integer sequence $X=(X_0,X_1,\\ldots,X_{N-1})$ as $\\displaystyle X_k = (Ak+B) \\bmod M$. Find the number of integers $k$ satisfying $0 \\le k < N$ and $X_k > k",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc443_g"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given integers $N,M,A,B$.\nDefine an integer sequence $X=(X_0,X_1,\\ldots,X_{N-1})$ as $\\displaystyle X_k = (Ak+B) \\bmod M$.\nFind the number of integers $k$ satisfying $0 \\le k < N$ and $X_k > k...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments