7-5 个人赛补题-Gym - 102263D D - Meeting Bahosain

Gym - 102263D  D - Meeting Bahosain 

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.

  1. Choose a number from the first array
  2. Add or subtract from it a number in the second array
  3. 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,mn,m (1≤n,k≤10^{6}) represent the length of the first and second array 

the second line contains n integers represent the first array (1≤a[i]≤{10^{9}})

the third line contains m integers represent the second array (1≤b[i]≤{10^{9}})

Output

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

题意:给你两个数组,让你判断能否让第一个数组(a数组)中的数通过加减第二个数组(b数组)的数,让第一个数组中的数全部相等。

(比赛的时候想法 已经很接近了,但是没有去深究一下)

思路:这是一道思维题,首先当然要读懂题意,因为他们是通过第一个数组加减第二个数组去实现,“抹平”他们直间的差值,也就是a[i]-a[i-1] 可以用b数组来表示。就可以写成a[i]-a[i-1] = b[1]*n_{1}+b[2]*n_{2}+b[3]*n_{3}+……+b[n-1]*n_{n-1}+b[n]*n_{n}; 这样就可以转换为a[i]-a[i-1]%(b数组的最打公约数)==0,从而判断是否可以。

 同样你也可以分别求一下两个数组的最大公约数,看第一个数组能否整除第二个数组。(特判n==1的情况)1e5以上输入卡cin和cout。

样例:

5 2
3 6 7 2 5
2 4

No

不多说了,上代码,自己悟。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <bitset>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+10;
const int mod=1e9+7;
typedef pair<char,char> PII;
int n,m;
string s;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll a[N],b[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    ll sum;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];
        if(i==1) sum=b[i];
        else sum=gcd(sum,b[i]);
    }
    int f=0;
    for (int i=2;i<=n;i++) {
        if ((a[i]-a[i-1])%sum!=0){
            f=1;break;
        }
    }
    if (!f)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <bitset>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+10;
const int mod=1e9+7;
typedef pair<char,char> PII;
int n,m;
string s;
ll gcd(ll a,ll b){//最大公因数模板
    return b?gcd(b,a%b):a;
}
ll a[N],b[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    ll sum1,sum2;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(i==1) sum1=a[i];
        else sum1=gcd(sum1,a[i]);
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];
        if(i==1) sum2=b[i];
        else sum2=gcd(sum2,b[i]);
    }
    if(sum1%sum2==0||n==1){
        printf("Yes\n");
    }else printf("No\n");
    //printf("%d\n%d\n",sum1,sum2);
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiYunLongYYDS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值