[原创]SPOJ PGCD - Primes in GCD Table [莫比乌斯反演+分段+求和优化]【组合数学】

2017-02-14 21:02:29 Tabris_ 阅读数:619


博客爬取于2020-06-14 22:41:33
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/55106268


题目连接:https://vjudge.net/problem/10581/origin

--------------------------------------------------------------------------------------------.
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 (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.

Input

First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 10^7.

Output

For each test case write one number - the number of prime numbers Johnny wrote in that test case.

Example

Input:
2
10 10
100 100

Output:
30
2791

--------------------------------------------------------------------------------------------.

题目大意:
问你在{gcd(x,y)x[1,n],y[1,m]}\{\gcd{(x,y)}\big | x \in [1,n] ,y\in [1,m]\}中素数的个数.

解题思路:
很好想到
f(d)=x=1ny=1m[gcd(x,y)=p]f(d) = \sum_{x=1}^{n} \sum_{y=1}^{m} \big [\gcd(x,y)=p \big] (其中P为素数,[] 括号内式子成立为1,否则0)
F(d)=x=1ny=1m[dgcd(x,y)]同时F(d)=[nd][md]F(d)=\sum_{x=1}^{n} \sum_{y=1}^{m} \big [d|\gcd(x,y)]\\同时 F(d) = \big[\dfrac{n}{d}\big]\big[\dfrac{m}{d}\big]

显然F(n)=dnf(d)F(n) = \sum_{d|n}f(d)

两种形式反演后得到
$f(d) = \sum _{d|n}\mu(\frac{n}{d})F(d) $ (1)
$f(n) = \sum _{d|n}\mu(d)F(\frac{n}{d}) $ (2)

我们取(1)式
f(d)=dnμ(nd)[nd][md]f(d) = \sum_{d|n}\mu(\frac{n}{d})\big[\dfrac{n}{d}\big]\big[\dfrac{m}{d}\big] (1)

最终结果就是

ans=pmin(n,m)f(p)=pmin(n,m)d=1min(n,m)/pμ(d)[n/pd][m/pd]ans = \sum_{p}^{min(n,m)}f(p) \\ =\sum_{p}^{min(n,m)}\sum_{d=1}^{min(n,m)/p}\mu({d})\big[\dfrac{n/p}{d}\big]\big[\dfrac{m/p}{d}\big]

直接计算复杂度太高 显然不可取。

令t = pk;
pmin(n,m)d=1min(n,m)/pμ(d)[nt][mt]=t=1min(n,m)pp<=n,pt,p为素数μ(tp)[nt][mt]\sum_{p}^{min(n,m)} \sum_{d=1}^{min(n,m)/p}\mu({d})\big[\dfrac{n}{t}\big]\big[\dfrac{m}{t}\big]\\=\sum_{t=1}^{min(n,m)} \sum_{p \big| p<=n,p|t,p为素数} \mu(\frac{t}{p})\big[\dfrac{n}{t}\big]\big[\dfrac{m}{t}\big]

其中对于pp<=n,pt,p为素数μ(tp)\sum_{p \big| p<=n,p|t,p为素数} \mu(\frac{t}{p})我们可以在线性筛中预处理出来,然后求其前缀和

最后通过分段优化一下即可,

参考

难死了,看了好久都不是很懂、,(Ps:线性筛真TM强大)

附本题代码
--------------------------------------------------------------------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int const MAX = 1e7 + 5;
int mob[MAX], p[MAX], g[MAX], sum[MAX];
bool noprime[MAX];

int Mobius()
{
mob[1] = 1;
int pnum = 0;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
g[i] = 1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
g[i * p[j]] = mob[i];
break;
}
mob[i * p[j]] = -mob[i];
g[i * p[j]] = mob[i] - g[i];
}
sum[i] = sum[i - 1] + g[i];
}
}

ll cal(int l, int r)
{
ll ans = 0;
if(l > r)
swap(l, r);
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = min(l / (l / i), r / (r / i));
ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
}

int main()
{
Mobius();
int T;
scanf("%d", &T);
while(T--)
{
int l, r;
scanf("%d %d", &l, &r);
printf("%lld\n", cal(l, r));
}
}