codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1155C.md (3299B)


      1 Source: https://codeforces.com/problemset/problem/1155/C
      2 
      3 C. Alarm Clocks Everywhere
      4 
      5 time limit per test
      6 
      7 3 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the $i$-th of them will start during the $x_i$-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes $x_1, x_2, \dots, x_n$, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).
     22 
     23 Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as $y$) and the interval between two consecutive signals (let's denote it by $p$). After the clock is set, it will ring during minutes $y, y + p, y + 2p, y + 3p$ and so on.
     24 
     25 Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of $p$. He has to pick it among the given values $p_1, p_2, \dots, p_m$ (his phone does not support any other options for this setting).
     26 
     27 So Ivan has to choose the first minute $y$ when the alarm clock should start ringing and the interval between two consecutive signals $p_j$ in such a way that it will ring during all given minutes $x_1, x_2, \dots, x_n$ (and it does not matter if his alarm clock will ring in any other minutes).
     28 
     29 Your task is to tell the first minute $y$ and the index $j$ such that if Ivan sets his alarm clock with properties $y$ and $p_j$ it will ring during all given minutes $x_1, x_2, \dots, x_n$ or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.
     30 
     31 Input
     32 
     33 The first line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5, 1 \le m \le 3 \cdot 10^5$) — the number of events and the number of possible settings for the interval between signals.
     34 
     35 The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^{18}$), where $x_i$ is the minute when $i$-th event starts. It is guaranteed that all $x_i$ are given in increasing order (i. e. the condition $x_1 \lt x_2 \lt \dots \lt x_n$ holds).
     36 
     37 The third line of the input contains $m$ integers $p_1, p_2, \dots, p_m$ ($1 \le p_j \le 10^{18}$), where $p_j$ is the $j$-th option for the interval between two consecutive signals.
     38 
     39 Output
     40 
     41 If it's impossible to choose such values $y$ and $j$ so all constraints are satisfied, print "NO" in the first line.
     42 
     43 Otherwise print "YES" in the first line. Then print two integers $y$ ($1 \le y \le 10^{18}$) and $j$ ($1 \le j \le m$) in the second line, where $y$ is the first minute Ivan's alarm clock should start ringing and $j$ is the index of the option for the interval between two consecutive signals (options are numbered from $1$ to $m$ in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes $x_1, x_2, \dots, x_n$. If there are multiple answers, you can print any.
     44 
     45 Examples
     46 
     47 Input
     48 
     49 ```
     50 3 5
     51 3 12 18
     52 2 6 5 3 3
     53 ```
     54 
     55 Output
     56 
     57 ```
     58 YES
     59 3 4
     60 ```
     61 
     62 Input
     63 
     64 ```
     65 4 2
     66 1 5 17 19
     67 4 5
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 NO
     74 ```
     75 
     76 Input
     77 
     78 ```
     79 4 2
     80 1 5 17 19
     81 2 1
     82 ```
     83 
     84 Output
     85 
     86 ```
     87 YES
     88 1 1
     89 ```