-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3336.cpp
More file actions
40 lines (40 loc) · 1.25 KB
/
Copy path3336.cpp
File metadata and controls
40 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int subsequencePairCount(vector<int>& nums) {
int M = *max_element(nums.begin(), nums.end());
int mod = 1e9 + 7;
vector<vector<int>> dp(M + 1, vector<int>(M + 1, 0));
vector<vector<int>> tempDP(M + 1, vector<int>(M + 1, 0));
dp[0][0] = 1;
for (auto& num : nums) {
// copy values
for (int i = M; i >= 0; --i) {
for (int j = M; j >= 0; --j) {
tempDP[i][j] = dp[i][j];
}
}
// update
for (int i = M; i >= 0; --i) {
for (int j = M; j >= 0; --j) {
int i2 = __gcd(num, i);
int j2 = __gcd(num, j);
int v = dp[i][j];
tempDP[i2][j] = (tempDP[i2][j] + v) % mod;
tempDP[i][j2] = (tempDP[i][j2] + v) % mod;
}
}
// copy back
for (int i = M; i >= 0; --i) {
for (int j = M; j >= 0; --j) {
dp[i][j] = tempDP[i][j];
}
}
}
int res = 0;
for (int i = 1; i <= M; ++i) {
res += dp[i][i];
res %= mod;
}
return res;
}
};