problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::count_array_pairs_divisible_by_k::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static long long coutPairs (vector< int > &nums, int k)
 

详细描述

在文件 leetcode.h1285 行定义.

成员函数说明

◆ coutPairs()

long long leetcode::count_array_pairs_divisible_by_k::Solution::coutPairs ( vector< int > &  nums,
int  k 
)
static

在文件 leetcode.cpp3243 行定义.

3243 {
3244 long long ans = 0;
3245 // 统计每个数的倍数出现的次数
3246 auto count = unordered_map<int, int>();
3247 for(const auto num: nums) {
3248 count[num]++;
3249 }
3250 const int maximum = *max_element(nums.begin(), nums.end());
3251 // 为什么这个算法是 O(nlnn) 的?因为这个算法的循环次数是 n(1 + 1/2 + 1/3 + ...),由调和级数可知括号内趋向 lnn
3252 for(int i = 1; i <= maximum; i++) {
3253 for(int j = i * 2; j <= maximum; j += i) {
3254 count[i] += count[j];
3255 }
3256 }
3257 for(const auto num: nums) {
3258 // 对于每个数统计与它形成好二元组的数有几个
3259 ans += count[k / gcd(k, num)];
3260 if(static_cast<long long>(num) * num % k == 0) {
3261 // 排除自己和自己形成好二元组的情况
3262 ans--;
3263 }
3264 }
3265 return ans / 2;
3266 }

被这些函数引用 leetcode::count_array_pairs_divisible_by_k::TEST().


该类的文档由以下文件生成: