Skip to content

Commit 2c8b51d

Browse files
Create Row GCD Explanation.txt
1 parent baa3a8f commit 2c8b51d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
- $g = \gcd(a_1 + b_i, a_2 + b_i, a_3 + b_i, \dots , a_n + b_i) = \gcd(a_1 + b_i, a_2 - a_1, a_3 - a_2, \dots , a_n - a_{n - 1})$
2+
- We will precalculate the GCD of the differences before hand so we can find the GCD in one step.
3+
4+
-----
5+
6+
int main()
7+
{
8+
int no_of_elements_a, no_of_elements_b;
9+
cin >> no_of_elements_a >> no_of_elements_b;
10+
11+
vector <long long> A(no_of_elements_a + 1), B(no_of_elements_b + 1);
12+
for(int i = 1; i <= no_of_elements_a; i++)
13+
{
14+
cin >> A[i];
15+
}
16+
17+
for(int i = 1; i <= no_of_elements_b; i++)
18+
{
19+
cin >> B[i];
20+
}
21+
22+
sort(all(A));
23+
24+
vector <long long> difference(no_of_elements_a);
25+
for(int i = 1; i < no_of_elements_a; i++)
26+
{
27+
difference[i] = A[i + 1] - A[i];
28+
}
29+
30+
long long array_gcd = 0;
31+
for(int i = 2; i < no_of_elements_a; i++)
32+
{
33+
array_gcd = gcd(array_gcd, difference[i]);
34+
}
35+
36+
vector <long long> answer(no_of_elements_b + 1);
37+
for(int i = 1; i <= no_of_elements_b; i++)
38+
{
39+
answer[i] = gcd(A[1] + B[i], array_gcd);
40+
}
41+
42+
for(int i = 1; i <= no_of_elements_b; i++)
43+
{
44+
cout << answer[i] << " ";
45+
}
46+
47+
cout << "\n";
48+
return 0;
49+
}

0 commit comments

Comments
 (0)