luogu2658 GCD(莫比乌斯反演/欧拉函数)

link

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对.

1<=N<=10^7

(1)莫比乌斯反演法

发现就是YY的GCD,左转YY的GCD粘过来就行

代码太丑,没开O2 TLE5个点

#include <cstdio>
#include <functional>
using namespace std;

const int fuck = 10000000;
int prime[10000010], tot;
bool vis[10000010];
int mu[10000010], sum[10000010];

int main()
{
    mu[1] = 1;
    for (int i = 2; i <= fuck; i++)
    {
        if (vis[i] == false) prime[++tot] = i, mu[i] = -1;
        for (int j = 1; j <= tot && i * prime[j] <= fuck; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0) break;
            mu[i * prime[j]] = -mu[i];
        }
    }
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j * prime[i] <= fuck; j++)
            sum[j * prime[i]] += mu[j];
    for (int i = 1; i <= fuck; i++)
        sum[i] += sum[i - 1];
    // int t; scanf("%d", &t);
    // while (t --> 0)
    // {
        int n, m;
        long long ans = 0; //别忘了初始化。。。
        scanf("%d", &n), m = n;
        if (n > m) {int t = m; m = n; n = t; }
        for (int i = 1, j; i <= n; i = j + 1)
        {
            j = min(n / (n / i), m / (m / i));
            ans += (sum[j] - sum[i - 1]) * (long long)(n / i) * (m / i);
        }
        printf("%lld\n", ans);
    // }
    return 0;
}

(2)欧拉函数法

对于一个\(p\)我们发现\(\sum_{i=1}^n\sum_{j=1}^n[\gcd(i,j)=p]\)即为\(\sum_{i=1}^{n/p}\sum_{j=1}^{n/p}[\gcd(i,j)=1]\)

左转SDOI仪仗队那题,发现这个式子就是\(2\varphi(\lfloor\frac n p\rfloor)+1\)

线性筛就行

(一个月前的代码

#include <bits/stdc++.h>
using namespace std;

int vis[10000010];
long long phi[10000010];
int prime[1000010], tot, n;

int main()
{
    cin >> n;
    phi[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        if (vis[i] == 0)
            prime[++tot] = i, phi[i] = i - 1;
        for (int j = 1; j <= tot && i * prime[j] <= n; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        }
        vis[i] ^= 1;
        vis[i] += vis[i - 1];
        phi[i] += phi[i - 1];
    }
    long long ans = 0;
    for (int i = 1; i <= tot; i++)
        ans += 2 *  phi[n / prime[i]] - 1;
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/oier/p/10295781.html

时间: 2024-08-26 15:59:17

luogu2658 GCD(莫比乌斯反演/欧拉函数)的相关文章

莫比乌斯反演欧拉函数杜教筛大总结

莫比乌斯函数 定义 设\(n=\prod_{i=1}^{k} p_i^{c_i}\),则\(\mu(n)=(-1)^k\),特别地\(\mu(1)=1\). 性质 最常用性质 \(\sum_{d|n}\mu(d)=[n=1]\) 反演性质 \(F(n)=\sum_{d|n}f(d) \Longleftrightarrow f(n)=\sum_{d|n}F(d)\mu(\frac{n}{d})\) \(F(n)=\sum_{n|d}f(d) \Longleftrightarrow f(n)=\su

HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解

题意: 给定\(n,m,p\),求 \[ \sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧拉函数性质可得:\(x,y\)互质则\(\varphi(xy)=\varphi(x)\varphi(y)\):\(p\)是质数则\(\varphi(p^a)=(p-1)^{a-1}\).因此,由上述两条性质,我们可以吧\(a,b\)质因数分解得到 \[ \begin{aligned} \sum_{

BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)

题目链接 \(Description\) 求\[\sum_{i=1}^n\gcd(i,n)\] \(Solution\) \[ \begin{aligned} \sum_{i=1}^n\gcd(i,n) &=\sum_{d=1}^nd\sum_{i=1}^n[\gcd(i,n)=d]\ &=\sum_{d=1}^nd\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}[\gcd(i,\lfloor\frac{n}{d}\rfloor)=1] \end{aligned

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

hdoj 1787 GCD Again【欧拉函数】

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2673    Accepted Submission(s): 1123 Problem Description Do you have spent some time to think and try to solve those unsolved problem af

UVA 11426 - GCD - Extreme (II) 欧拉函数-数学

Given the value of N, you will have to ?nd the value of G. The de?nition of G is given below:G =i<N∑i=1j∑≤Nj=i+1GCD(i, j)Here GCD(i, j) means the greatest common divisor of integer i and integer j.For those who have trouble understanding summation no

UVA11426 GCD - Extreme (II)---欧拉函数的运用

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=473&problem=2421&mosmsg=Submission+received+with+ID+13800900 Given the value of N, you will have to ?nd the value of G. The de?nition

HDOJ 1787 GCD Again(欧拉函数)

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2611    Accepted Submission(s): 1090 Problem Description Do you have spent some time to think and try to solve those unsolved problem a

UVa 11426 GCD - Extreme (II) (欧拉函数应用&#183;O(N*logN))

题意  令  G(n) = sum{gcd(i, j) | 0 < i < n, i < j <= n}  给你一个n  输出G(n) 令 F(n) = sum{gcd(i, n) | 0 < i < n}  那么有递推式 G(n) = G(n-1) + F(n) , G(0)  = 0  也就是说只用求出F(n) 就能递推求出 G(n)了  而求F(n)就比较容易了 对于i  设 x < i , gcd(x,i) = 1 即x, n 互质 则  gcd(2*x,