(转载)有关反演和gcd

tips :

积性函数 F (n) = Π F (piai )

若F (n), G (n)是积性函数则

F (n) * G (n)

Σd | n F (n)

是积性函数

n = Σd | n  φ (d)

1 = Σd | n  μ (d)

Σgcd (i, n) = 1 i = n * φ (n) / 2

Problem1

F (n) = Σ1<= i <= n gcd(i, n), n <= 1000000

Sol

枚举结果

F (n) = Σd | n d * Σgcd (i, n) = d 1

F (n) = Σd | n d * Σgcd (i / d, n / d) = 1 1

F (n) = Σd | n d * Σgcd (i / d, n / d) = 1 1

F (n) = Σd | n d * φ (n / d)

单次计算O (sqrt N)

筛法O (N)

Problem 2

F (n) = Σ1<= i <= n gcd (i, n), n <= 2147483647               (POJ longge‘s problem)

Sol

由P1可知F (n)是积性函数 因此考虑计算F (pk)

由P1 易知

F (pk) = p * F (pk - 1)  + (p - 1)pk - 1

单个F(n)可以O (sqrt N)时间有他的质因子分解计算得到

Problem 3

F (n) = Σ1<= i <= n lcm(i, n), n <= 1000000             (SPOJ LCMSUM)

显而易见的变形

F (n) = Σ1<= i <= n i * n / gcd (i, n)

F (n) =Σd | n  Σgcd (i, n) = d i * n / d

F (n) =Σd | n n / d * Σgcd (i, n) = d i

F (n) =Σd | n n / d * Σgcd (i / d, n / d) = 1 i

F (n) =Σd | n n / d * d * Σgcd (i / d, n / d) = 1 i / d

令j = i / d

F (n) =Σd | n n * Σgcd (j, n / d) = 1 j

由Σgcd (i, n) = 1 i = n * φ (n) / 2

F (n) =Σd | n n * (n / d) * φ (n / d) / 2

F (n) =n * Σd | n (n / d) * φ (n / d) / 2

F (n) =n / 2 * Σd | n d * φ (d)

筛出 Σd | n d * φ (d)     O (N)-O(1)

Problem 4

F (n) = Σ1<= i <= n Σ1<= j <= n gcd (i, j)      n <= 1000000          (SPOJ GCDEX)

Sol

G (n) = Σd | n d * φ (n / d)

F (n) = Σ1<= i <= n G (i)

筛出G (i) 前缀和

Problem 5

求F (n, m) = [n / d] * [m / d]

Sol

研究退化情况 m = 1 F (n) = [n / d]

共有sqrt n种不同取值

F (n, m) = [n / d] * [m / d]

共有sqrt n + sqrt m种不同取值  归并这两种取值

Problem 6

多组询问n, m 求F (n, m) = Σ1<= i <= n Σ1<= j <= m gcd (i, j)

Sol

F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ d | gcd (i, j) φ (d)

F (n, m) = Σ d φ (d) Σ1<= i <= n d | i Σ1<= j <= m d | j 1

F (n, m) = Σ d φ (d) * [n / d] * [m / d]

可以经P5解决

Problem 6

多组询问n, m 求F (n, m) = Σ1<= i <= n Σ1<= j <= m gcd (i, j) = 1 1

Sol

F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ d | gcd (i, j) μ (d)

F (n, m) = Σ d μ (d) Σ1<= i <= n d | i Σ1<= j <= m d | j 1

F (n, m) = Σ d μ (d) * [n / d] * [m / d]

可以经P5解决

Problem 7

F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ gcd (i, j) <- prime 1            (BZOJ YY的GCD)

Sol

F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ gcd (i, j) <- prime 1

F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ p <- prime gcd (i, j) = p 1

F (n, m) = Σ p <- prime Σ1<= i <= n Σ1<= j <= m  gcd (i, j) = p 1

F (n, m) = Σ p <- prime Σ1<= i <= n / p Σ1<= j <= m / p gcd (n / p, m / p) = 1  1

可以经P6解决

(转载)有关反演和gcd

时间: 2024-11-06 20:27:02

(转载)有关反演和gcd的相关文章

BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 2534  Solved: 1129 [Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2)

(转载)关于gcd的8题

发现其实有关gcd的题目还是挺多的,这里根据做题顺序写出8题. [bzoj2818: Gcd] gcd(x,y)=质数, 1<=x,y<=n的对数 做这题的时候,懂得了一个非常重要的转化:求(x, y) = k, 1 <= x, y <= n的对数等于求(x, y) = 1, 1 <= x, y <= n/k的对数!所以,枚举每个质数p(线性筛素数的方法见:线性时间内筛素数和欧拉函数),然后求(x, y) = 1, 1 <= x, y <= n/p的个数.

SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)

PGCD - Primes in GCD Table Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greate

hdu_1695: GCD 【莫比乌斯反演】

题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就好了.注意要删去重复的. 关于 莫比乌斯反演 的结论 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=1e6; int prime[maxn+5]; bool check[maxn+

bnu——GCD SUM (莫比乌斯反演)

题目:GCD SUM 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=39872 算法:莫比乌斯反演.优化 1 #include<stdio.h> 2 #define N 100001 3 typedef long long LL; 4 bool pri[N]={0}; 5 int prim[N],po=0; 6 int mu[N]; 7 LL f[N],ff[N]; //缩短时间 8 /* 9 莫比乌斯函数mu[i]的定义: 10 1. 如

HDU 1695 (莫比乌斯反演) GCD

题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是不知道怎么应用. 这里有关于莫比乌斯反演的知识,而且最后的例题中就有这道题并给出了公式的推导. 1 #include <cstdio> 2 #include <algorithm> 3 typedef long long LL; 4 5 const int maxn = 1000000

hdu-1695 GCD(莫比乌斯反演)

题目链接: GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor

BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discuss] Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必然不会了,于是向你来请教……多组输入 Input 第一行一个整数T 表述数据组数接下来T行,每行两个正

acdream 1148 GCD SUM 莫比乌斯反演 ansx,ansy

GCD SUM Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)