E - GuGuFishtion HDU - 6390(欧拉函数 / 莫比乌斯反演)

GuGuFishtion (HDU - 6390)

题意:

定义\(G_u (a,b)=\frac{\phi(ab)}{\phi(a)\phi(b)}\)。

求\((\sum\limits_{a=1}^m\sum\limits_{b=1}^nG_u (a,b))\pmod p\)。

题解:

考虑\(\phi(x) = x*(1-\frac{1}{p_1})*(1-\frac{1}{p_2})...*(1-\frac{1}{p_n})\)。

将\(G_u (a,b)\)的分子与分母按上述分解、约分后,得\(\frac{1}{(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_3})...(1-\frac{1}{p_k})}\),其中\(p_1*p_2*p_3...p_k=gcd(a,b)\)。

上下同时乘以\(gcd(a,b)\),则\(G_u (a,b)=\frac{gcd(a,b)}{\phi(gcd(a,b))}\)。

问题变成了求\((\sum\limits_{a=1}^m\sum\limits_{b=1}^n\frac{gcd(a,b)}{\phi(gcd(a,b))})\pmod p\)。

上式 \(=(\sum_d^{min(n, m)}\frac{d}{\phi(d)}\sum\limits_{a=1}^m\sum\limits_{b=1}^n[gcd(a,b)==d])\pmod p\)。

对于\(\sum\limits_{a=1}^m\sum\limits_{b=1}^n[gcd(a,b)==d]\),即\(\sum\limits_{a=1}^{m/d}\sum\limits_{b=1}^{n/d}[gcd(a,b)==1]\),可以用莫比乌斯反演求。

代码:

#include <bits/stdc++.h>
#define fopi freopen("in.txt", "r", stdin)
#define fopo freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef long double ld;
const int maxn = 1e6 + 10;

int check[maxn], phi[maxn], mu[maxn], prime[maxn];
LL inv[maxn], sum[maxn];

void init(int N) {
    memset(check, false, sizeof(check));
    mu[1] = 1;
    phi[1] = 1;
    int tot = 0;
    for (int i = 2; i <= N; i++) {
        if (!check[i]) {
            prime[tot++] = i;
            mu[i] = -1;
            phi[i] = i-1;
        }
        for (int j = 0; j < tot; j++) {
            if (i * prime[j] > N) break;
            check[i * prime[j]] = true;
            if (i % prime[j] == 0) {
                mu[i * prime[j]] = 0;
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            else {
                mu[i * prime[j]] = -mu[i];
                phi[i * prime[j]] = phi[i] * (prime[j]-1);
            }
        }
    }
}

LL solve(int n, int m, int p) {
    LL res = 0;
    for (int i = 1, la = 0; i <= m; i = la+1) {
        la = min(n/(n/i), m/(m/i));
        res = (res + 1ll * (sum[la] - sum[i-1]) * (n/i) % p * (m/i) % p) % p;
    }
    return res;
}

int T, n, m, p;
int main() {
    init(maxn - 10);

    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d", &n, &m, &p);
        if (n < m) swap(n, m);

        inv[1] = 1;
        for (int i = 2; i <= m; i++)
            inv[i] = 1ll * inv[p % i] * (p - p/i) % p;
        for (int i = 1; i <= m; i++)
            sum[i] = (sum[i-1] + mu[i] + p) % p;

        LL ans = 0;
        for (int i = 1; i <= m; i++) {
            ans = (ans + 1ll * i * inv[phi[i]] % p * solve(n/i, m/i, p) % p) % p;
        }

        printf("%lld\n", ans);
    }
}

原文地址:https://www.cnblogs.com/ruthank/p/11366435.html

时间: 2024-10-08 21:06:00

E - GuGuFishtion HDU - 6390(欧拉函数 / 莫比乌斯反演)的相关文章

hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不会 就自己写了个容斥搞一下(才能维持现在的生活) //别人的题解https://blog.csdn.net/luyehao1/article/details/81672837 #include <iostream> #include <cstdio> #include <cstr

hdu6390 /// 欧拉函数+莫比乌斯反演 筛inv[] phi[] mu[]

题目大意: 给定m n p 求下式   题解:https://blog.csdn.net/codeswarrior/article/details/81700226 莫比乌斯讲解:https://www.cnblogs.com/peng-ym/p/8647856.html 莫比乌斯的mu[]:https://www.cnblogs.com/cjyyb/p/7953803.html #include <bits/stdc++.h> using namespace std; #define LL

【模版】线性筛(素数,欧拉函数,莫比乌斯函数)

线性筛: 线性筛是一种比较实用的筛法,它与数论中的(完全)积性函数密切相关: (完全)积性函数的定义:对于两个整数 \(x_1\) 和 \(x_2\) ,若有函数\(f(x)\)满足:\(f(x_1x_2)=f(x_1)f(x_2)\),我们称\(f(x)\)为完全积性函数:特殊的:若 \(x_1\) 和 \(x_2\) 一定为两个互质的正整数,我们称\(f(x)\)为积性函数! 而线性筛就是利用了这一性质,将\(f(x)\)用且只用\(x\)最小的那个质因子利用\(f(x_1x_2)=f(x_

hdu 3307(欧拉函数+好题)

Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1071    Accepted Submission(s): 323 Problem Description an = X*an-1 + Y and Y mod (X-1) = 0.Your task is to cal

HDU 1286 欧拉函数。

[科普]什么是BestCoder?如何参加? 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8077    Accepted Submission(s): 4250 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是

hdu 4983(欧拉函数)

Goffi and GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 992    Accepted Submission(s): 336 Problem Description Goffi is doing his math homework and he finds an equality on his text book: g

hdu 2824(欧拉函数)

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5235    Accepted Submission(s): 2225 Problem Description The Euler function phi is an important kind of function in number theo

hdu 2588 欧拉函数

两个数的gcd为d,其实就是将这两个数同除以d后互质.本题中n是固定的,x是小于等于n的数,很容易想到可以枚举n的约数求出(n除以约数)的欧拉函数的和即是答案. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 8 int euler_phi( int n ) 9 { 10 int ans =

hdu 3501 欧拉函数

容易想到容斥原理,但是结合欧拉函数的公式,我们得到: 小于n且与n互质的数的和为:n * phi(n) / 2 于是问题迎刃而解. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 const int MOD = 1000000007; 8 9 int euler_phi( int n ) 10