Skip to content

Instantly share code, notes, and snippets.

@balamark
Last active June 4, 2017 19:17
Show Gist options
  • Save balamark/da5df88221a499172c6084f8471b04cd to your computer and use it in GitHub Desktop.
Save balamark/da5df88221a499172c6084f8471b04cd to your computer and use it in GitHub Desktop.
SRM413 ArithmeticProgression
#define MAX 128
class ArithmeticProgression {
public:
double minCommonDifference(int a0, vector <int> a)
{
int N = SIZE(a);
if(N == 0)
return 0.0;
if(a[0] < a0)
return -1.0;
double Min = a[0] - a0;
double Max = Min + 1.0;
int i, j;
FOR(i, 0, MAX)
{
double Mid = (Min + Max)/2;
FOR(j, 0, N)
if(floor(a0 + (j + 1)*Mid) < a[j])
break;
if(j < N)
Min = Mid;
else
Max = Mid;
}
FOR(i, 0, N)
if(int(floor(a0 + (i + 1)*Max)) != a[i])
break;
if(i < N)
return -1.0;
return Max;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment