spoj Primes in GCD Table 莫比乌斯反演
来源:互联网 发布:工商总局打击网络传销 编辑:程序博客网 时间:2024/05/18 21:06
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 < 107.
Output
For each test case write one number - the number of prime numbers Johnny wrote in that test case.
Example
Input:210 10100 100Output:302791
莫比乌斯的理解见 传送门
题意:
要求求出gcd(x , y)为质数的个数,x ,y的区间见题
题解:
令 f(p)=gcd(x,y)为p (p为质数)
F(pk)=gcd(x,y)>=p (k为质数)
公式借鉴 传送门
直接这样做会TLE,那么考虑优化,令t=pk
我们便可以将
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 10000000#define LL long longbool check[MAXN+10];int primer[MAXN+10];int mu[MAXN+10];int sum[MAXN+10];void Moblus(){ memset(check,0,sizeof(check)); mu[1]=1; int tot=0; for(int i=2;i<=MAXN;i++){ if(!check[i]){ primer[tot++]=i; mu[i]=-1; } for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){ check[i*primer[j]]=true; if(i%primer[j]==0){ mu[i*primer[j]]=0; break; } mu[i*primer[j]]=-mu[i]; } } /// sum[0]=0; for(int i=0;i<tot;i++) for(int j=primer[i];j<MAXN;j+=primer[i]) sum[j]+=mu[j/primer[i]]; for(int i=1;i<MAXN;i++) sum[i]+=sum[i-1];}LL solve(int n,int m){ LL ans=0; if(n>m) swap(n,m); for(int i=1,last;i<=n;i=last+1){ last=min(n/(n/i),m/(m/i)); ans+=(LL)(sum[last]-sum[i-1])*(n/i)*(m/i); } return ans;}int main(){ int T; Moblus(); int a,b; freopen("in.txt","r",stdin); scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); LL ans=solve(a,b); printf("%lld\n",ans); } return 0;}
- spoj Primes in GCD Table 莫比乌斯反演
- spoj 4491. Primes in GCD Table 莫比乌斯反演
- SPOJ 4491 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 PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)
- SPOJ PGCD - Primes in GCD Table [莫比乌斯反演+分段+求和优化]【组合数学】
- SPOJ4491 Primes in GCD Table 莫比乌斯反演+分块+前缀和
- SPOJ-PGCD Primes in GCD Table (Mobius反演 好题)
- SPOJ 4491. Primes in GCD Table 【mobius反演】
- SPOJ 4491 Primes in GCD Table
- spoj 4491 莫比乌斯反演
- BZOJ 2818 Gcd + spoj 4491(莫比乌斯反演 分块)
- HDU1695 GCD(莫比乌斯反演)
- GCD问题--莫比乌斯反演
- POJ 1013 Counterfeit Dollar 找假硬币 模拟
- CC2640R2F BLE5.0 CC2640R2F增加一个具有通知属性的characteristic
- com:向对象到面向服务
- org.apache.commons.lang3功能示例
- 打印一个字符串的所有排列。
- spoj Primes in GCD Table 莫比乌斯反演
- C语言
- Hibernate(十二)命名查询-批量处理和调用存储过程
- Java 集合系列13之 WeakHashMap详细介绍(源码解析)和使用示例
- 文件上传
- ios 11 CORE ML 学习入门
- 侯二牛推荐的书。
- maven 环境变量
- fatal error C1075: 与左侧的 圆括号“)”