AtCoder Beginner Contest 109 C题

C - Skip


Time limit : 2sec / Memory limit : 1024MB

Score : 300 points

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate xi.

Your objective is to visit all these cities at least once.

In order to do so, you will first set a positive integer D.

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

  • Move 1: travel from coordinate y to coordinate y+D.
  • Move 2: travel from coordinate y to coordinate yD.

Find the maximum value of D that enables you to visit all the cities.

Here, to visit a city is to travel to the coordinate where that city is located.

Constraints

  • All values in input are integers.
  • 1≤N≤105
  • 1≤X≤109
  • 1≤xi≤109
  • xi are all different.
  • x1,x2,…,xNX

Input

Input is given from Standard Input in the following format:

N X
x1 x2 … xN

Output

Print the maximum value of D that enables you to visit all the cities.


Sample Input 1

Copy

3 3
1 7 11

Sample Output 1

Copy

2

Setting D=2 enables you to visit all the cities as follows, and this is the maximum value of such D.

  • Perform Move 2 to travel to coordinate 1.
  • Perform Move 1 to travel to coordinate 3.
  • Perform Move 1 to travel to coordinate 5.
  • Perform Move 1 to travel to coordinate 7.
  • Perform Move 1 to travel to coordinate 9.
  • Perform Move 1 to travel to coordinate 11.

题意:从X出发,要求最大的D,保证每个点都能走到,可以看出其实就是求每个点距离X的距离的最大公约数;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include<map>
typedef long long ll;
using namespace std;
void read(int &x){int f=1;x=0;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}x*=f;}
int GCD(int a,int b){  
return b==0?a:GCD(b,a%b);
}
int main(){	
	int n,m;
	int a[100005];
	int b[100005];
	read(n);
	read(m);
	int h=0;
	int top=0;
	for(int i=0;i<n;i++){
		read(a[i]);
		b[top++]=abs(a[i]-m);
	}
	h=GCD(b[0],b[1]);
	for(int i=2;i<n;i++){
		h=GCD(b[i],h);
	}
	printf("%d\n",h);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值