AcWing 1246. 等差数列
原题链接
中等
作者:
一抹斜阳
,
2020-02-09 11:45:34
,
所有人可见
,
阅读 978
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
int a[N];
int res;
int gcd(int a,int b);
int main()
{
int n;
cin>>n;
int i = 0;
for(i = 0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int d = a[1] - a[0];
for(i = 1;i<n;i++)
{
d = gcd(d,a[i] - a[i-1]);
if(d == 1 || d == 0)
break;
}
if(d != 0)
res = (a[n-1]-a[0])/d + 1;
else
res = n;
cout<<res<<endl;
return 0;
}
int gcd(int a,int b)
{
if(b == 0)
return a;
return gcd(b,a%b);
}