add: 230405-CN

This commit is contained in:
Eatswap 2023-04-05 14:50:26 +08:00
parent c79ce48e37
commit 5d896a3452
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 26 additions and 1 deletions

25
cpp/2304/230405-CN.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <numeric>
/**
* 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;
}

View File

@ -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)