Alarm Clocks Everywhere

版权声明:《本文为博主原创文章,转载请注明出处。 》 https://blog.csdn.net/zbq_tt5/article/details/89466072
  • time limit per test
  • 3 seconds
  • memory limit per test
  • 256 megabytes
  • input
  • standard input
  • output
  • standard output

output

standard output

Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the ii-th of them will start during the xixi-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 x1,x2,…,xnx1,x2,…,xn, 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).

Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as yy) and the interval between two consecutive signals (let's denote it by pp). After the clock is set, it will ring during minutes y,y+p,y+2p,y+3py,y+p,y+2p,y+3p and so on.

Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of pp. He has to pick it among the given values p1,p2,…,pmp1,p2,…,pm (his phone does not support any other options for this setting).

So Ivan has to choose the first minute yy when the alarm clock should start ringing and the interval between two consecutive signals pjpj in such a way that it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn (and it does not matter if his alarm clock will ring in any other minutes).

Your task is to tell the first minute yy and the index jj such that if Ivan sets his alarm clock with properties yy and pjpj it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.

Input

The first line of the input contains two integers nn and mm (2≤n≤3⋅105,1≤m≤3⋅1052≤n≤3⋅105,1≤m≤3⋅105) — the number of events and the number of possible settings for the interval between signals.

The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤10181≤xi≤1018), where xixi is the minute when ii-th event starts. It is guaranteed that all xixi are given in increasing order (i. e. the condition x1<x2<⋯<xnx1<x2<⋯<xn holds).

The third line of the input contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pj≤10181≤pj≤1018), where pjpj is the jj-th option for the interval between two consecutive signals.

Output

If it's impossible to choose such values yy and jj so all constraints are satisfied, print "NO" in the first line.

Otherwise print "YES" in the first line. Then print two integers yy (1≤y≤10181≤y≤1018) and jj (1≤j≤m1≤j≤m) in the second line, where yy is the first minute Ivan's alarm clock should start ringing and jj is the index of the option for the interval between two consecutive signals (options are numbered from 11 to mm 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 x1,x2,…,xnx1,x2,…,xn. If there are multiple answers, you can print any.

Examples

input

3 5
3 12 18
2 6 5 3 3

output

YES
3 4

input

4 2
1 5 17 19
4 5

output

NO

input

4 2
1 5 17 19
2 1

output

YES
1 1

题意:

给定一串连续递增的时间,还有一串可以设置的闹钟时间间隔,现在问是否存在一个时间间隔,使得能够使闹钟在所有的规定时间都能响。

题解:

数据范围是3e5*3e5,如果直接暴力的话会超时,可以先寻找每两个时间间隔之间的最大公约数,如果最大公约数能够整除时间间隔的话,就说明这个数据成立。

gcd()函数:

求最大公约数,使用的时候要加上头文件<algorithm>

使用方法:__gcd(x,y);

限制范围:x和y的数据类型相同,还有就是不能对浮点型数据进行操作。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int n,m;
    cin>>n>>m;
    long long a[n];
    for(int i=0; i<n; ++i)
        cin>>a[i];
    long long  gcd=a[1]-a[0];
    for(int i=2; i<n; ++i)
    {
        gcd=__gcd(gcd,a[i]-a[i-1]);
    }
    int flag=0;
    long long b[m];
    for(int i=0; i<m; ++i)
    {
        cin>>b[i];
        if(gcd%b[i]==0&&flag==0)
        {
            printf("YES\n%lld %d\n",a[0],i+1);
            flag=1;
        }
    }
    if(!flag)
    printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zbq_tt5/article/details/89466072