C. Alarm Clocks Everywhere(欧几里得算法简单应用)

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
inputCopy
3 5
3 12 18
2 6 5 3 3
outputCopy
YES
3 4
inputCopy
4 2
1 5 17 19
4 5
outputCopy
NO
inputCopy
4 2
1 5 17 19
2 1
outputCopy
YES
1 1

题目大意:给你n个起始时间和m个闹钟,对于每个闹钟,你可以定义它的开始响的时间y,然后它会每隔pi分钟响一下,问你是否能提醒这n个时间,如果能请随意输出一组y和选择的闹钟的位置

y就是x[0].然后就是求出来n-1个差,然后求出来最大公约数,然后判断这m个间隔是否存在一个是这个数的因子即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 3e5+10;
#define  ll long long
ll x[maxn];
ll p[maxn];
ll dis[maxn];

int main()
{
    int n,m;
    cin>>n>>m;
    int i;
    int j;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&x[i]);
        if(i!=0)
            dis[i] = x[i] - x[0];
    }
    for(i=0; i<m; i++)
    {
        scanf("%lld",&p[i]);
    }
    ll t = __gcd(dis[1],dis[2]);
    for(i=3; i<n; i++)
    {
        t = __gcd(t,dis[i]);
    }
    for(i=0; i<m; i++)
    {
        if(t%p[i]==0&&t>=p[i])
            break;
    }
    if(i!=m)
        printf("YES\n%lld %d\n",x[0],i+1);
    else
        printf("NO\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值