# UVa 1642-magical GCD (number theory)

Source: Internet
Author: User

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4517

Test instructions

Enter a sequence of positive integers for an n (n≤100000) element, and a continuous sub-sequence so that the greatest common divisor of all elements in the sequence is the largest product of the sequence length.
For example, the 5 elements of the sequence 30, 60, 20, 20, 20 of the optimal solution is {60, 20, 20, 20}, the product is gcd (60,20,20,20) *4=80.

Analysis:

The right boundary J of the sequence is enumerated from left to right, and the left boundary I≤j is quickly obtained, making MGCD (I,J) the largest.
where MGCD (I,J) is defined as GCD (a[i],a[i+1],..., a[j]) * (j-i+1).
Consider sequence 5, 8, 6, 2, 6, 8, when j=5 need to compare I=1, 2, 3, 4, 5 o'clock MGCD (I,J), as shown in the following table:

I=1,GCD expression =gcd (5,8,6,2,6), gcd value = 1, sequence length = 5.
I=2,GCD expression =gcd (8,6,2,6), gcd value = 2, sequence length = 4.
I=3,GCD expression =gcd (6,2,6), gcd value = 2, sequence length = 3.
I=4,GCD expression =gcd (2,6), gcd value = 2, sequence length = 2.
I=5,GCD expression =GCD (6), gcd value = 6, sequence length = 1.

From the bottom up, the GCD expression is one more element at a time, sometimes gcd, sometimes smaller, and each time the hour becomes a certain number of it.
In other words, there are only logj of different GCD values! When the GCD value is the same, the larger the sequence length the better, so you can simplify the table:

GCD value =1,i=1.
GCD value =2,i=2.
GCD value =6,i=5.

Since there are only LOGJ elements in the table, it is possible to compare each I-MGCD (i,j) in turn, with a time complexity of O (LOGJ).
Let's consider how the table will change when J is changed from 5 to 6 o'clock.
First, all of the above GCD values are then a6=8 gcd, and then to join the I=6 project, the GCD value is 8.
Because the same GCD value only needs to retain the minimum value of I, i=5 is deleted and finally gets the result as shown in the following table.

GCD value =1,i=1.
GCD value =2,i=2.
GCD value =8,i=6.

The total time complexity is O (NLOGN).

Code:

```
1 #include <cstdio>
2 #include <vector>
3 using namespace std;
4
5 typedef long long int LLI;
6 struct Item {
7     LLI g;
8     int p;
9 };
10
11 LLI gcd(LLI a, LLI b) {
12     return b == 0 ? a : gcd(b, a%b);
13 }
14
15 int main() {
16     int T, n;
17     LLI v, ans;
18     scanf("%d", &T);
19     while(T--) {
20         ans = 0;
21         vector<Item> vec;
22         scanf("%d", &n);
23         for(int t = 0; t < n; t++) {
24             scanf("%lld", &v);
25             for(int i = 0; i < vec.size(); i++) vec[i].g = gcd(vec[i].g, v);
26             vec.push_back((Item){v,t});
27             vector<Item> nvec;
28             for(int i = 0; i < vec.size(); i++) {
29                 if(i != 0 && vec[i].g == vec[i-1].g) continue;
30                 ans = max(ans, vec[i].g * (t-vec[i].p+1));
31                 nvec.push_back(vec[i]);
32             }
33             vec = nvec;
34         }
35         printf("%lld\n", ans);
36     }
37     return 0;
38 }
```

UVa 1642-magical GCD (number theory)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.