博客
关于我
NYOJ 1066 CO-PRIME(数论)
阅读量:800 次
发布时间:2023-02-17

本文共 1819 字,大约阅读时间需要 6 分钟。

为了解决这个问题,我们需要计算给定数组中互素数对的数量。互素数对指的是两个数的最大公约数为1的对。我们可以利用莫比乌斯函数和容斥原理来高效地解决这个问题。

方法思路

  • 莫比乌斯函数:这个函数用于帮助我们通过容斥原理来计算互素数对的数量。莫比乌斯函数 mu(d) 的值取决于 d 的质因数分解情况:

    • 如果 d 有平方因子,mu(d) = 0
    • 否则,mu(d)(-1)^k,其中 kd 的不同质因数的个数。
  • 容斥原理:我们可以利用莫比乌斯函数来计算互素数对的数量。具体来说,互素数对的数量可以表示为:[\text{总对数} = \sum_{d=1}^{max_a} \mu(d) \times C(\text{cnt}[d], 2)]其中,cnt[d] 表示数组中被 d 整除的数的个数,C(cnt[d], 2) 是从 cnt[d] 个数中选出两个的组合数。

  • 步骤

    • 预计算莫比乌斯函数。
    • 统计每个数的频率。
    • 计算每个 dcnt[d],即数组中被 d 整除的数的个数。
    • 计算互素数对的数量并输出结果。
  • 解决代码

    #include 
    #include
    #include
    #include
    using namespace std;const int MAXN = 100010;int mu[MAXN];void compute_mobius(int n) { mu[1] = 1; vector
    is_prime(n + 1, true); is_prime[1] = false; for (int i = 2; i <= n; ++i) { if (is_prime[i]) { for (int j = i; j <= n; j += i) { is_prime[j] = false; mu[j] = -mu[j / i]; if (j % i == 0) { mu[j] = 0; } } } }}int main() { compute_mobius(MAXN); while (~scanf("%d", &n)) { vector
    a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } int max_a = *max_element(a.begin(), a.end()); vector
    freq(max_a + 1, 0); for (int num : a) { freq[num]++; } vector
    cnt(max_a + 1, 0); for (int d = 1; d <= max_a; ++d) { for (int j = d; j <= max_a; j += d) { cnt[d] += freq[j]; } } long long ans = 0; for (int d = 1; d <= max_a; ++d) { if (cnt[d] >= 2) { ans += (cnt[d] * (cnt[d] - 1) / 2) * mu[d]; } } cout << ans << endl; } return 0;}

    代码解释

  • 预计算莫比乌斯函数:使用筛法预计算 mu 数组,标记每个数的质因数情况。
  • 读取输入:读取每个测试用例的数据,包括数组 a
  • 频率统计:统计数组中每个数的出现次数。
  • 计算 cnt 数组:统计每个 d 的倍数出现次数。
  • 计算互素数对数量:利用莫比乌斯函数和容斥原理计算互素数对的数量并输出结果。
  • 这种方法高效地解决了问题,能够在合理的时间和空间复杂度内处理大规模输入。

    转载地址:http://eznfk.baihongyu.com/

    你可能感兴趣的文章
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>