B. Mahmoud the Thief

Codeforces
IDCF10203B
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
As Ayoub was going home from Mahmoud's house, he remembered that he forgot his hard disk, which contains the solutions to $n$ problems from the JU Flash contest. The $i^(t h)$ problem has size $a_i$. While panicking, he remembered that he installed a software that prevented the user from copying any files and only allowed the files to be cut from the hard disk in any order the user wants. As a security measure, the software will stop the transfer if the number of remaining files is 1, or the absolute difference between the sizes of every pair of problems left on the hard disk is divisible by $m$. Ayoub knows the permutation $b$, which represents the order of the problems Mahmoud will attempt to cut from the hard disk. Since Ayoub is a bad programmer, he asks for your help to tell him how many problems Mahmoud will steal until the transfer stops. The first line contains two integer $n$ and $m$ $(1 <= n <= 10^5)$ $(1 <= m <= 10^9)$ The second line contains integers $a_1$,$a_2$,...,$a_n$ $(1 <= a_i <= 10^9)$ The third line contains a permutation $b$ , $b_1$,$b_2$,...,$b_n$ $(1 <= b_i <= 10^5)$ print exactly one integer $-$ the number of codes Mahmoud can steal In The first sample : At first Mahmoud will try to cut the file with index $2$, and he can do it since there exists two files, the absolute difference between their sizes is not divisible by $m$(for example file $2$ and file $1$ ($a_1 -a_2 = 1$) and $1$ is not divisible by $2$). After cutting The first file, the remaining files will be the files with indices $1$ and $3$, Mahmoud can't cut The next file, because the absolute difference between any pair is divisible by $m$. So Mahmoud will steal only one file. ## Input The first line contains two integer $n$ and $m$ $(1 <= n <= 10^5)$ $(1 <= m <= 10^9)$The second line contains integers $a_1$,$a_2$,...,$a_n$ $(1 <= a_i <= 10^9)$The third line contains a permutation $b$ , $b_1$,$b_2$,...,$b_n$ $(1 <= b_i <= 10^5)$ ## Output print exactly one integer $-$ the number of codes Mahmoud can steal [samples] ## Note In The first sample :At first Mahmoud will try to cut the file with index $2$, and he can do it since there exists two files, the absolute difference between their sizes is not divisible by $m$(for example file $2$ and file $1$ ($a_1 -a_2 = 1$) and $1$ is not divisible by $2$).After cutting The first file, the remaining files will be the files with indices $1$ and $3$, Mahmoud can't cut The next file, because the absolute difference between any pair is divisible by $m$.So Mahmoud will steal only one file.
**Definitions** Let $ n, m \in \mathbb{Z}^+ $. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of positive integers representing file sizes. Let $ b = (b_1, b_2, \dots, b_n) $ be a permutation of $ \{1, 2, \dots, n\} $, representing the order of attempted file removals. **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. $ 1 \leq m \leq 10^9 $ 3. $ 1 \leq a_i \leq 10^9 $ for all $ i \in \{1, \dots, n\} $ 4. $ b $ is a permutation of $ \{1, \dots, n\} $ **Objective** Simulate the process: - Start with the full set of files $ S = \{1, 2, \dots, n\} $. - For $ k = 1 $ to $ n $: - Let $ i = b_k $ be the next file to attempt removal. - If $ |S| = 1 $, stop. - Else if for all distinct $ j, \ell \in S \setminus \{i\} $, $ m \mid |a_j - a_\ell| $, stop. - Else, remove $ i $ from $ S $ and continue. - Output the number of files successfully removed before stopping.
API Response (JSON)
{
  "problem": {
    "name": "B. Mahmoud the Thief",
    "description": {
      "content": " As Ayoub was going home from Mahmoud's house, he remembered that he forgot his hard disk, which contains the solutions to $n$ problems from the JU Flash contest. The $i^(t h)$ problem has size $a_i$.",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10203B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "As Ayoub was going home from Mahmoud's house, he remembered that he forgot his hard disk, which contains the solutions to $n$ problems from the JU Flash contest. The $i^(t h)$ problem has size $a_i$.\n...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of positive integers representing file sizes.  \nLet $ b = (b_1, b_2, \\dots, b_n) $ be a permutation ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments