
Input: nums =[6,10,3]Output: 5Explanation: The figure shows all the non-empty subsequences and their GCDs.The different GCDs are 6,10,3,2, and 1.
For each possible GCD g, check if there exists a subsequence whose GCD is g. For g to be a GCD, there must be numbers in nums that are multiples of g, and their GCD can be reduced to g by picking appropriate elements. Use a sieve-like approach to check for each g from 1 to max(nums).
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int countDifferentSubsequenceGCDs(vector<int>& nums) {
int mx =*max_element(nums.begin(), nums.end());
vector<bool> present(mx+1, false);
for (int x : nums) present[x] = true;
int ans =0;
for (int g =1; g <= mx; ++g) {
int cur =0;
for (int m = g; m <= mx; m += g) {
if (present[m]) cur = gcd(cur, m);
}
if (cur == g) ++ans;
}
return ans;
}
intgcd(int a, int b) { return b ? gcd(b, a % b) : a; }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
funccountDifferentSubsequenceGCDs(nums []int) int {
mx:=0for_, x:=rangenums { ifx > mx { mx = x } }
present:= make([]bool, mx+1)
for_, x:=rangenums { present[x] = true }
ans:=0gcd:=func(a, bint) int { forb!=0 { a, b = b, a%b }; returna }
forg:=1; g<=mx; g++ {
cur:=0form:=g; m<=mx; m+=g {
ifpresent[m] { cur = gcd(cur, m) }
}
ifcur==g { ans++ }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
classSolution {
publicintcountDifferentSubsequenceGCDs(int[] nums) {
int mx = Arrays.stream(nums).max().getAsInt();
boolean[] present =newboolean[mx+1];
for (int x : nums) present[x]=true;
int ans = 0;
for (int g = 1; g <= mx; ++g) {
int cur = 0;
for (int m = g; m <= mx; m += g) {
if (present[m]) cur = gcd(cur, m);
}
if (cur == g) ++ans;
}
return ans;
}
intgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funcountDifferentSubsequenceGCDs(nums: IntArray): Int {
val mx = nums.maxOrNull()!!val present = BooleanArray(mx+1)
for (x in nums) present[x] = truevar ans = 0fungcd(a: Int, b: Int): Int = if (b ==0) a else gcd(b, a % b)
for (g in1..mx) {
var cur = 0var m = g
while (m <= mx) {
if (present[m]) cur = gcd(cur, m)
m += g
}
if (cur == g) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from math import gcd
classSolution:
defcountDifferentSubsequenceGCDs(self, nums: List[int]) -> int:
mx = max(nums)
present = set(nums)
ans =0for g in range(1, mx+1):
cur =0for m in range(g, mx+1, g):
if m in present:
cur = gcd(cur, m)
if cur == g:
ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashSet;
impl Solution {
pubfncount_different_subsequence_gcds(nums: Vec<i32>) -> i32 {
let mx =*nums.iter().max().unwrap();
let present: HashSet<i32>= nums.into_iter().collect();
letmut ans =0;
for g in1..=mx {
letmut cur =0;
letmut m = g;
while m <= mx {
if present.contains(&m) { cur = gcd(cur, m); }
m += g;
}
if cur == g { ans +=1; }
}
ans
}
}
fngcd(a: i32, b: i32) -> i32 { if b ==0 { a } else { gcd(b, a % b) } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functioncountDifferentSubsequenceGCDs(nums: number[]):number {
constmx= Math.max(...nums);
constpresent=newSet(nums);
letans=0;
for (letg=1; g<=mx; ++g) {
letcur=0;
for (letm=g; m<=mx; m+=g) {
if (present.has(m)) cur=gcd(cur, m);
}
if (cur===g) ans++;
}
returnans;
}
functiongcd(a: number, b: number):number { returnb===0?a : gcd(b, a%b); }