add: 220406 [cpp]

This commit is contained in:
Lam Haoyin 2022-04-06 15:29:18 +08:00
parent 1172d4842b
commit f6badffceb
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 44 additions and 1 deletions

43
cpp/2204/220406.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <vector>
#include <iostream>
class Solution {
private:
static inline const int MAX_VALUE = 100 + 2;
static inline const int MOD = 1000000007;
inline static unsigned long long CX2(unsigned long long n) {
return (n & 1) ? ((n - 1 >> 1) * n) : ((n >> 1) * (n - 1));
}
inline static unsigned long long CX3(unsigned long long n) {
return (n - 2) * CX2(n) / 3;
}
public:
static int threeSumMulti(const std::vector<int>& arr, const int target) {
unsigned long long cnt[3 * MAX_VALUE]{};
for (int i: arr)
++cnt[i];
unsigned long long ret = 0;
// 3 Different values
for (int i = 0; i < MAX_VALUE; ++i)
for (int j = i + 1; j < (target - i - j); ++j)
ret = (ret + (((cnt[target - i - j] * cnt[i]) % MOD) * cnt[j]) % MOD) % MOD;
// 2 Same Values
for (int i = 0; i < MAX_VALUE && target - i - i >= 0; ++i)
if (cnt[i] >= 2 && i + i + i != target)
ret = (ret + CX2(cnt[i]) * cnt[target - i - i] % MOD) % MOD;
// 3 Same values
return (ret + (3 * (target / 3) == target ? CX3(cnt[target / 3]) : 0)) % MOD;
}
};
int main() {
std::cout << Solution::threeSumMulti({0,0,0}, 0);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220404-CN.cpp) ADD_EXECUTABLE(2204 220406.cpp)