From f6badffceb610977a3d1e7f7094b1817c8e524a8 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Wed, 6 Apr 2022 15:29:18 +0800 Subject: [PATCH] add: 220406 [cpp] --- cpp/2204/220406.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220406.cpp diff --git a/cpp/2204/220406.cpp b/cpp/2204/220406.cpp new file mode 100644 index 0000000..d5de496 --- /dev/null +++ b/cpp/2204/220406.cpp @@ -0,0 +1,43 @@ +#include +#include + +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& 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; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 68c4c84..2779ee4 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220404-CN.cpp) +ADD_EXECUTABLE(2204 220406.cpp)