这题感觉用gcd(最大公约数)可以做
//洛谷应该没有这题
题目描述
在数轴上,一共有 n 个景点,坐标分别为 x 1 , x 2 , x 3 , … ....,x n
你初始在 x=0位置,每次你可以往左 d 个单位或往右 d 个单位,请问为了访问到每一个景点, d 最大可以取到多少?
输入格式
输入共两行: 第一行,第一个正整数 n
第二行, n个整数 x 1 , x 2 , x 3 , … . , � � x 1 ,x 2 ,x 3 ,…,x n
输出格式
输出一行,表示答案
数据范围 对于 30 % 30%的数据, 1≤n≤10
对于 60%的数据, 1≤n≤10^3
对于 100 % 100%的数据, 1≤n≤10^5 , −10^9≤x1,x2…xn≤10^9
样例输入
2 -4 4
3 -2 4 10
样例输出
4
2
思路
看样例发现输出都是所有数的绝对值的最大公约数,那就很简单了,上代码:
#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<ctime>
#include<queue>
#include<string>
#include<vector>
#define sc scanf
#define pr printf
using namespace std;
int n,a[100010];
int gcd(int x,int y)
{
if(x%y==0)
{
return y;
}
return gcd(x,x%y);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]<0)
{
abs(a[i]);
}
}
int t=a[1];
for(int i=2;i<=n;i++)
{
t=gcd(t,a[i]);
}
cout<<abs(t)<<endl;
return 0;}