1 - GCD - the greatest common factor.
The algorithm for searching GCD 2 numbers is known, but I will give it again.
long long gcd(long long a,long long b){ while (a && b) if (a > b) a%=b; else b%=a; return a+b; }
It uses the assumption that the НОД(0,0)=0 , it is often convenient.
Now, in order to find the GCD of several numbers, you need to consistently take the GCD from them.
long long gcd(const vector<long long>& num){ if (num.size() == 0) return 0; if (num.size() == 1) return num[0]; long long res = gcd(num[0],num[1]); for (auto i=2;i<num.size() && res!=1;i++) //&& res!=1 можно не писать res = gcd(res,num[i]); }
find the numbers, the division into which all elements of the set give the same remainder.
There are different options for how to do this. But the idea you have to go from 2 to the GCD is wrong. Example: 5 and 9 give the remainder 1 when divided by 4. But GCD (5.9) = 1.
If there is more than 1 number in the set (I assume that all numbers are different ), then the number of such divisors will be no more than the minimum of these numbers. But it is a long time.
Let x = a*p + r, y = b*p + r Then xy = (ba)*p And it means that it is divisible by p for the other pairs. Therefore, the answer is all the dividers from the GCD of the differences of all numbers (There are N ^ 2, but all are optional, it is enough between neighboring numbers or from 1 to all, the total is N-1).
Evidence. Let G = gcd(delt) .
Then if x ≡ r (mod G). y = x + (y - x) => y = x + G*q => y ≡ r + G*q (mod G) => y ≡ r (mod G). x ≡ r (mod G). y = x + (y - x) => y = x + G*q => y ≡ r + G*q (mod G) => y ≡ r (mod G). As required.
Inverse: для любых (x,y) x ≡ r (mod Q) y ≡ r (mod Q) G not | Q => xy ≡ 0 (mod Q) => xy делится на Q. Отсюда противоречие с тем что G - gcd(); для любых (x,y) x ≡ r (mod Q) y ≡ r (mod Q) G not | Q => xy ≡ 0 (mod Q) => xy делится на Q. Отсюда противоречие с тем что G - gcd();
Realization add yourself.
Complexity - memory O(n) or O(1) time O(n * log2(M) + sqrt(M) ) . where M is the maximum value. sqrt(M) - factoring the answer, you can easily find algorithms without it.