From 5d896a3452bb1b5d5b9ec6b12adc2958fe70aabd Mon Sep 17 00:00:00 2001 From: Eatswap Date: Wed, 5 Apr 2023 14:50:26 +0800 Subject: [PATCH] add: 230405-CN --- cpp/2304/230405-CN.cpp | 25 +++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 cpp/2304/230405-CN.cpp diff --git a/cpp/2304/230405-CN.cpp b/cpp/2304/230405-CN.cpp new file mode 100644 index 0000000..476701c --- /dev/null +++ b/cpp/2304/230405-CN.cpp @@ -0,0 +1,25 @@ +#include +#include + +/** + * 2427. Number of Common Factors + * + * Given two positive integers a and b, return the number of common factors of a and b. + * An integer x is a common factor of a and b if x divides both a and b. + */ + +class Solution { +public: + static constexpr inline int commonFactors(int a, int b) { + int x = std::gcd(a, b), ret = 0; + for (int i = 1; i * i <= x; ++i) + ret += ((x % i == 0) << 1) - (i * i == x); + return ret; + } +}; + +int main() { + std::cout << Solution::commonFactors(452, 392); + std::cout << std::gcd(452, 392); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index e14ecf6..dbe10aa 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230404.cpp) +ADD_EXECUTABLE(2304 230405-CN.cpp)