cf题解--D - Meeting Bahosain

https://codeforces.com/group/5yyKg9gx7m/contest/277016/problem/D

D. Meeting Bahosain
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Essa wanted to meet the most powerful number theorist of all time: Bahosain, but Bahosain does not waste his precious time, so he gave Essa this puzzle in order to test his abilities.

Given two arrays, the second array only has distinct elements, Essa can do the following as many times as he wants to make all numbers in first array equal.

Choose a number from the first array
Add or subtract from it a number in the second array
Replace the number in the first array with the result
Of course, Essa is unworthy of meeting the almighty Bahosain, and he can't solve this puzzle on his own, so can you help him?
Input
The first line containing two space separated integers n,m (1≤n,k≤1e6) represent the length of the first and second array.

the second line contains n integers represent the first array (1≤a[i]≤1e9)

the third line contains m integers represent the second array (1≤b[i]≤1e9)

Output
Print Yes, if it's possible to make all numbers in first array equal; and No in the opposite case.

Example
inputCopy
5 2
3 6 7 2 5
2 4
outputCopy
No

题目描述:

给两个数组a和b。对a的每一个数可以加或减b的任何数任意次,问能否使a的数全相等。

分析:

对于每个a[i]-a[j]的差能不能通过b的任意组合表示出来,即a[i]-a[j]=b1*x1+b2*x2+...+bn*xn是否存在整数解(x1,x2...xn)。由ax+by=gcd(a,b)一定有解可得,设g为b1,b2...bn的最小公倍数,只要a[i]-a[j]是g的整倍数,一定有解。
代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e6+6;
int a[maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    int gcd=0;
    for(int i=1;i<=k;i++)
    {
        int b;
        scanf("%d",&b);
        if(i==1) gcd=b;
        else gcd=__gcd(gcd,b);
    }
    bool ok=1;
    for(int i=1;i<n;i++)
    {
        int d=a[i]-a[i-1];
        if(d%gcd!=0) 
        {
            ok=0;
            break;
        }
    }
    if(ok) puts("Yes\n");
    else puts("No\n");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12751876.html