Educational Codeforces Round 63 (Rated for Div. 2)C. Alarm Clocks Everywhere【数论】

C. Alarm Clocks Everywhere

思路:题意是给你n个任务,你可以任意选择一个开始时间,并从给定序列中选择一个间隔,使得能完成这n个任务。

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

#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
ll a[maxn], p[maxn];
ll d[maxn];
ll gcd(ll x, ll y)
{
    return y == 0 ? x : gcd(y, x % y);
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%I64d", &a[i]);
    }
    for(int i = 2; i <= n; ++i)
    {
        d[i] = a[i] - a[i - 1];
    }
    ll ans = gcd(d[2], d[3]);
    for(int i = 4; i <= n; ++i)
    {
        ans = gcd(ans, d[i]);
    }
    for(int i = 1; i <= m; ++i)
    {
        scanf("%I64d", &p[i]);
    }
    ll y, pos;
    bool flag = false;
    for(int i = 1; i <= m; ++i)
    {
        if(ans % p[i] == 0)
        {
            pos = i;
            flag = true;
            break;
        }
    }
    if(flag)
    {
        printf("YES\n");
        printf("%I64d %I64d\n", a[1], pos);
    }
    else
        printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/89480952