AtCoder Beginner Contest 109 C - Skip

题目链接

题意
数字线上有N个城市。第i个城市位于坐标x处,目标是至少访问所有这些城市一次。
您将首先设置一个正整数D。
您将离开坐标X,并执行下面的移动1和移动2,任意次数:

移动1:从坐标y移动到坐标y+D。
移动2:从坐标y移动到坐标y−D

找出D的最大值,使您能够访问所有城市。

分析
可以发现想要走到每个点,两个相邻的点的距离必定是D的整数倍。
所以只需要处理出序列的距离,并求出整个数列的最大公约数。
注意一开始的位置也要加入序列中。

#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double, double> PDD;

const double PI = acos(-1.0);
const int N = 1e5 + 10,M = N * 2,INF = 0x3f3f3f3f;
const LL mod = 1e9  + 7;

int h[N],e[2 * N],ne[2 * N],idx;
int dx[4] = {-1,0,1,0},dy[4] = {0,-1,0,1}; 
//int dx[8] = {-1,-1,-1,0,0,1,1,1},dy[8] = {1,0,-1,-1,1,1,0,-1}; 
priority_queue<int> q;
//priority_queue<int, vector<int>, greater<int>> q;
map<int,int> ma;

int qmi(int a,int b,int p)
{
    int ans = 1;
    while(b)
    {
        if(b & 1) ans = (LL)ans * a % p;
        b >>= 1;
        a = (LL) a * a % p;
    }
    return ans;
}

int gcd(int a,int b){return b?gcd(b,a%b):a;}

int T;
int n,m;
LL a[N],b[N];
bool st[N];

int main(){
	scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++){
    	scanf("%lld",&a[i]);
    }
    a[n + 1] = m;
    sort(a + 1,a + 1 + n + 1);
    for(int i = 1;i < n + 1;i ++){
    	b[i] = a[i + 1] - a[i];
    }
    LL t = b[1];
    for(int i = 2;i <= n;i ++){
    	t = gcd(t,b[i]);
    }
    cout<<t<<endl;
    return 0;
}	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值