SPOJ PGCD - Primes in GCD Table [莫比乌斯反演+分段+求和优化]【组合数学】
来源:互联网 发布:mx anywhere 2 软件 编辑:程序博客网 时间:2024/05/16 01:39
题目连接: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
——————————————————————————————–.
题目大意:
问你在
解题思路:
很好想到
设
设
显然
两种形式反演后得到
我们取(1)式
最终结果就是
直接计算复杂度太高 显然不可取。
令t = pk;
其中对于
最后通过分段优化一下即可,
参考
难死了,看了好久都不是很懂、,(Ps:线性筛真TM强大)
附本题代码
——————————————————————————————–.
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)); } }
- SPOJ PGCD - Primes in GCD Table [莫比乌斯反演+分段+求和优化]【组合数学】
- SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)
- 【莫比乌斯反演】[SPOJ-PGCD]Primes in GCD Table
- SPOJ - PGCD Primes in GCD Table - 莫比乌斯反演
- 【SPOJ PGCD】Primes in GCD Table——莫比乌斯反演2
- SPOJ 4491 PGCD - Primes in GCD Table(莫比乌斯反演)
- SPOJ PGCD - Primes in GCD Table (莫比乌斯)
- spoj Primes in GCD Table 莫比乌斯反演
- SPOJ-PGCD Primes in GCD Table (Mobius反演 好题)
- spoj 4491. Primes in GCD Table 莫比乌斯反演
- SPOJ 4491 Primes in GCD Table(莫比乌斯反演)
- SPOJ4491 Primes in GCD Table 莫比乌斯反演+分块+前缀和
- SPOJ 4491. Primes in GCD Table 【mobius反演】
- 【莫比乌斯函数】【SPOJ 4491】PGCD
- SPOJ 4491 Primes in GCD Table
- DLUTOJ 1330 GCD 【莫比乌斯反演+组合】
- bzoj2301 莫比乌斯反演+分段优化
- 莫比乌斯反演对gcd问题的优化
- Log4j日志入门
- 看来还的一步一步来,首先要了解wpf的关键技术
- win10环境下虚拟机安装ubuntu系统
- 详解Java解析XML的四种方法
- [Leetcode] #39#40#216 Combination Sum I & II & III
- SPOJ PGCD - Primes in GCD Table [莫比乌斯反演+分段+求和优化]【组合数学】
- Mybatis多对多关联查询
- 算法提高 最大乘积
- 适配器模式——Adapter
- 01-【介绍说明篇】Hello,ArcGIS网络分析
- switch 小记
- oracle常用命令
- oracle 12c 创建公共用户和本地用户
- NOIP2016普及组[魔法阵]题解