Alarm Clocks Everywhere(1155C)

Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the ?-th of them will start during the ??-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 ?1,?2,…,??, 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 ?) and the interval between two consecutive signals (let's denote it by ?). After the clock is set, it will ring during minutes ?,?+?,?+2?,?+3? and so on.

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

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

Your task is to tell the first minute ? and the index ? such that if Ivan sets his alarm clock with properties ? and ?? it will ring during all given minutes ?1,?2,…,?? 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 ? and ? (2≤?≤3⋅105,1≤?≤3⋅105) — the number of events and the number of possible settings for the interval between signals.

The second line of the input contains ? integers ?1,?2,…,?? (1≤??≤1018), where ?? is the minute when ?-th event starts. It is guaranteed that all ?? are given in increasing order (i. e. the condition ?1<?2<⋯<?? holds).

The third line of the input contains ? integers ?1,?2,…,?? (1≤??≤1018), where ?? is the ?-th option for the interval between two consecutive signals.

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

Otherwise print "YES" in the first line. Then print two integers ? (1≤?≤1018) and ? (1≤?≤?) in the second line, where ? is the first minute Ivan's alarm clock should start ringing and ? is the index of the option for the interval between two consecutive signals (options are numbered from 1 to ? 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 ?1,?2,…,??. 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

题意:n个工作开始的时间点,m个可以选择的时间间隔,定闹钟每隔一个时间间隔响一次,问可以定一次闹钟就能在每个工作开始的时间点醒来吗,如果能,输出起始时间点和选的时间间隔。

思路:计算所有相邻工作起始时间点差的最大公约数,如果在m个数中存在一个时间能被最大公约数整除,就输出YES

坑:n==1和n==2的时候,如何计算很多数的最大公约数。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int M=3e5+10;
const int MAX=1e18+10;
const double PI = acos(-1);
ll a[M],b[M],c[M];
ll gcd(ll p,ll q)
{
    if(q==0)
        return p;
    else
        return gcd(q,p%q);
}
int main()
{
    ll n,m,i,j,g=MAX,e=0,h;
    scanf("%lld%lld",&n,&m);
    scanf("%lld",&a[0]);
    for(i=1;i<n;i++)
    {
        scanf("%lld",&a[i]);
        c[e++]=a[i]-a[i-1];
    }
    if(e==1)
    {
        g=a[1]-a[0];
    }
    else
    if(e>1)
       g=gcd(c[0],c[1]);
    for(i=2;i<e;i++)
    {
        g=gcd(g,c[i]);
    }
    ll flag=0,loca=-1;
    for(i=0;i<m;i++)
    {
        scanf("%lld",&b[i]);
        h=g%b[i];
        if(h==0)
        {
            flag=1;
            loca=i;
        }
    }
    if(n==1)
    {
        printf("YES\n");
        printf("%lld %lld\n",a[0],b[0]);
        return 0;
    }
    if(flag==0)
        printf("NO\n");
    else
    {
        printf("YES\n");
        printf("%lld %lld\n",a[0],loca+1);
    }
    return 0;
}



//2 1
//1 999999998000000002
//999999999

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值