[M数学] lc3164. 优质数对的总数 II(因数分解+倍增+推公式+思维+好题)

文章目录

    • [1. 题目来源](#1. 题目来源)
    • [2. 题目解析](#2. 题目解析)

1. 题目来源

链接:3164. 优质数对的总数 II

2. 题目解析

挺不错的一道 因数分解、倍增 的题目,需要一定的思维和推公式的能力才能解决。灵神的题解已经非常清晰易懂了,可以直接去看。

倍增思路:

  • 枚举 num1、nums2 每个数出现的次数。
  • 再枚举 nums2 * k 的倍数,如果在 nums1 中有出现,则基于乘法原理,两个次数相乘累加结果。
  • 倍数累计的上界即为 nums1 中的最大值。

分解因数思路:

  • 考虑,nums1i % (nums2j * k) == 0 则有,(nums1i / k) % nums2j == 0
  • 即,nums1i 首先是 k 的倍数,且 nums1i/k 存在因子 nums2j
  • 那么可以针对 nums1i/k 分解它的各个因子,并记录个数,此时 cnta=b 则等价于有 b 个 numsi/k 存在因子 a
  • 枚举每一个 nums2,答案累加 cntnums2\[j] 即可。

具体的,见灵神题解,很清楚了:


这个东西分析有点难度,见灵神的分析吧...


因数分解代码:

cpp 复制代码
class Solution {
public:
    long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        typedef long long LL;
        unordered_map<int, int> h;
        for (auto x : nums1) {
            if (x % k) continue;
            x /= k;
            for (int i = 1; i <= x / i; i ++ ) {
                if (x % i) continue;
                h[i] ++ ;
                if (i * i < x) h[x / i] ++ ;
            }
        }

        LL res = 0;
        for (int x : nums2) res += h[x];
        return res;
    }
};

倍增代码:

cpp 复制代码
class Solution {
public:
    long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        typedef long long LL;
        unordered_map<int, int> cnt1, cnt2;
        for (auto x : nums1) 
            if (x % k == 0)
                cnt1[x / k] ++ ;
        
        for (auto x : nums2) cnt2[x] ++ ;

        int u = -1;
        for (auto [k, v] : cnt1) u = max(u, k);

        LL res = 0;
        for (auto [x, cnt] : cnt2 ) {
            int s = 0;
            for (int y = x; y <= u; y += x) s += cnt1[y];

            res += 1ll * s * cnt;
        }
        return res;
    }
};
相关推荐
Tisfy1 小时前
LeetCode 1927.求和游戏:抵消+看最值
java·leetcode·游戏·题解·博弈论
玖玥拾1 小时前
LeetCode 125 验证回文串
算法·leetcode
玖玥拾11 小时前
LeetCode 392 判断子序列
笔记·算法·leetcode
重生之后端学习12 小时前
239. 滑动窗口最大值[困难]✅
java·数据结构·算法·leetcode·职场和发展
INGNIGHT21 小时前
1584.连接所有点的最小费用(最小生成树&并查集union find)
c++·leetcode
wabs66621 小时前
关于二叉树【力扣144.二叉树的前序遍历的思考】
数据结构·c++·算法·leetcode·二叉树
Xin7701 天前
LeetCode 23.合并 K 个升序链表(分治递归)
leetcode
Nil2082 天前
leetcode 105从前序和中序遍历序列构造二叉树
算法·leetcode·职场和发展
Nil2082 天前
leetcode 114二叉树展开为链表
leetcode·链表·深度优先
cz07102 天前
hot100_搜索二维矩阵 II
算法·leetcode