@@ -56,27 +56,217 @@ It can be shown that we can not select any other subarray with a gcd-sum greater
56
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def maxGcdSum (self , nums : List[int ], k : int ) -> int :
61
+ s = list (accumulate(nums, initial = 0 ))
62
+ f = []
63
+ ans = 0
64
+ for i, v in enumerate (nums):
65
+ g = []
66
+ for j, x in f:
67
+ y = gcd(x, v)
68
+ if not g or g[- 1 ][1 ] != y:
69
+ g.append((j, y))
70
+ f = g
71
+ f.append((i, v))
72
+ for j, x in f:
73
+ if i - j + 1 >= k:
74
+ ans = max (ans, (s[i + 1 ] - s[j]) * x)
75
+ return ans
60
76
```
61
77
62
78
### ** Java**
63
79
64
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
81
66
82
``` java
67
-
83
+ class Solution {
84
+ public long maxGcdSum (int [] nums , int k ) {
85
+ int n = nums. length;
86
+ long [] s = new long [n + 1 ];
87
+ for (int i = 1 ; i <= n; ++ i) {
88
+ s[i] = s[i - 1 ] + nums[i - 1 ];
89
+ }
90
+ List<int[]> f = new ArrayList<> ();
91
+ long ans = 0 ;
92
+ for (int i = 0 ; i < n; ++ i) {
93
+ List<int[]> g = new ArrayList<> ();
94
+ for (var e : f) {
95
+ int j = e[0 ], x = e[1 ];
96
+ int y = gcd(x, nums[i]);
97
+ if (g. isEmpty() || g. get(g. size() - 1 )[1 ] != y) {
98
+ g. add(new int [] {j, y});
99
+ }
100
+ }
101
+ f = g;
102
+ f. add(new int [] {i, nums[i]});
103
+ for (var e : f) {
104
+ int j = e[0 ], x = e[1 ];
105
+ if (i - j + 1 >= k) {
106
+ ans = Math . max(ans, (s[i + 1 ] - s[j]) * x);
107
+ }
108
+ }
109
+ }
110
+ return ans;
111
+ }
112
+
113
+ private int gcd (int a , int b ) {
114
+ return b == 0 ? a : gcd(b, a % b);
115
+ }
116
+ }
68
117
```
69
118
70
119
### ** C++**
71
120
72
121
``` cpp
73
-
122
+ class Solution {
123
+ public:
124
+ long long maxGcdSum(vector<int >& nums, int k) {
125
+ int n = nums.size();
126
+ long long s[ n + 1] ;
127
+ s[ 0] = 0;
128
+ for (int i = 1; i <= n; ++i) {
129
+ s[ i] = s[ i - 1] + nums[ i - 1] ;
130
+ }
131
+ vector<pair<int, int>> f;
132
+ long long ans = 0;
133
+ for (int i = 0; i < n; ++i) {
134
+ vector<pair<int, int>> g;
135
+ for (auto [ j, x] : f) {
136
+ int y = gcd(x, nums[ i] );
137
+ if (g.empt() || g.back().second != y) {
138
+ g.emplace_back(j, y);
139
+ }
140
+ }
141
+ f = move(g);
142
+ f.emplace_back(i, nums[ i] );
143
+ for (auto [ j, x] : f) {
144
+ if (i - j + 1 >= k) {
145
+ ans = max(ans, (s[ i + 1] - s[ j] ) * x);
146
+ }
147
+ }
148
+ }
149
+ return ans;
150
+ }
151
+ };
74
152
```
75
153
76
154
### **Go**
77
155
78
156
```go
157
+ func maxGcdSum(nums []int, k int) int64 {
158
+ n := len(nums)
159
+ s := make([]int64, n+1)
160
+ s[0] = 0
161
+ for i, x := range nums {
162
+ s[i+1] = s[i] + int64(x)
163
+ }
164
+ type pair [2]int
165
+ var f []pair
166
+ var ans int64
167
+ for i := 0; i < n; i++ {
168
+ var g []pair
169
+ for _, p := range f {
170
+ j, x := p[0], p[1]
171
+ y := int(gcd(int(x), nums[i]))
172
+ if len(g) == 0 || g[len(g)-1][1] != y {
173
+ g = append(g, pair{j, y})
174
+ }
175
+ }
176
+ f = g
177
+ f = append(f, pair{i, nums[i]})
178
+ for _, p := range f {
179
+ j, x := p[0], p[1]
180
+ if i-j+1 >= k {
181
+ ans = max(ans, (s[i+1]-s[j])*int64(x))
182
+ }
183
+ }
184
+ }
185
+ return ans
186
+ }
187
+
188
+ func gcd(a, b int) int {
189
+ if b == 0 {
190
+ return a
191
+ }
192
+ return gcd(b, a%b)
193
+ }
194
+ ```
195
+
196
+ ### ** TypeScript**
197
+
198
+ ``` ts
199
+ function maxGcdSum(nums : number [], k : number ): number {
200
+ const n: number = nums .length ;
201
+ const s: number [] = Array (n + 1 ).fill (0 );
202
+ for (let i = 1 ; i <= n ; i ++ ) {
203
+ s [i ] = s [i - 1 ] + nums [i - 1 ];
204
+ }
205
+
206
+ let f: [number , number ][] = [];
207
+ let ans: number = 0 ;
208
+
209
+ for (let i = 0 ; i < n ; ++ i ) {
210
+ const g: [number , number ][] = [];
211
+ for (const [j, x] of f ) {
212
+ const y: number = gcd (x , nums [i ]);
213
+ if (g .length === 0 || g .at (- 1 )[1 ] !== y ) {
214
+ g .push ([j , y ]);
215
+ }
216
+ }
217
+ f = g ;
218
+ f .push ([i , nums [i ]]);
219
+ for (const [j, x] of f ) {
220
+ if (i - j + 1 >= k ) {
221
+ ans = Math .max (ans , (s [i + 1 ] - s [j ]) * x );
222
+ }
223
+ }
224
+ }
225
+
226
+ return ans ;
227
+ }
228
+
229
+ function gcd(a : number , b : number ): number {
230
+ return b === 0 ? a : gcd (b , a % b );
231
+ }
232
+ ```
79
233
234
+ ### ** TypeScript**
235
+
236
+ ``` ts
237
+ function maxGcdSum(nums : number [], k : number ): number {
238
+ const n: number = nums .length ;
239
+ const s: number [] = Array (n + 1 ).fill (0 );
240
+ for (let i = 1 ; i <= n ; i ++ ) {
241
+ s [i ] = s [i - 1 ] + nums [i - 1 ];
242
+ }
243
+
244
+ let f: [number , number ][] = [];
245
+ let ans: number = 0 ;
246
+
247
+ for (let i = 0 ; i < n ; ++ i ) {
248
+ const g: [number , number ][] = [];
249
+ for (const [j, x] of f ) {
250
+ const y: number = gcd (x , nums [i ]);
251
+ if (g .length === 0 || g .at (- 1 )[1 ] !== y ) {
252
+ g .push ([j , y ]);
253
+ }
254
+ }
255
+ f = g ;
256
+ f .push ([i , nums [i ]]);
257
+ for (const [j, x] of f ) {
258
+ if (i - j + 1 >= k ) {
259
+ ans = Math .max (ans , (s [i + 1 ] - s [j ]) * x );
260
+ }
261
+ }
262
+ }
263
+
264
+ return ans ;
265
+ }
266
+
267
+ function gcd(a : number , b : number ): number {
268
+ return b === 0 ? a : gcd (b , a % b );
269
+ }
80
270
```
81
271
82
272
### ** ...**
0 commit comments