SPOJ PGCD(莫比乌斯反演)
题意:给定两个数和
,其中
,
,求
为质数的
有多少对?其中
和
的范围是
。
分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0).
- #pragma comment(linker,"/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <cmath>
- #include <limits.h>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <cstdlib>
- #include <stack>
- #include <vector>
- #include <set>
- #include <map>
- #define LL long long
- #define mod 100000000
- #define inf 0x3f3f3f3f
- #define eps 1e-6
- #define N 10000000
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define PII pair<int,int>
- using namespace std;
- inline int read()
- {
- char ch=getchar();int x=,f=;
- while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
- while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- bool vis[N+];
- int mu[N+],prime[N+],sum[N+],num[N+];
- void Mobius()
- {
- memset(vis,false,sizeof(vis));
- mu[]=;
- int tot=;
- for(int i=;i<=N;i++)
- {
- if(!vis[i])
- {
- prime[tot++]=i;
- mu[i]=-;
- }
- for(int j=;j<tot;j++)
- {
- if(i*prime[j]>N)break;
- vis[i*prime[j]]=true;
- if(i%prime[j]==)
- {
- mu[i*prime[j]]=;
- break;
- }
- else
- {
- mu[i*prime[j]]=-mu[i];
- }
- }
- }
- for(int i=;i<tot;i++)
- for(int j=prime[i];j<=N;j+=prime[i])
- num[j]+=mu[j/prime[i]];//预处理出对于所有质数p,sigma(f(p))对应的F(i)的系数,用num[i]表示
- for(int i=;i<=N;i++)sum[i]=sum[i-]+num[i];
- }
- LL solve(int n,int m)
- {
- LL res=;
- if(n>m)swap(n,m);
- for(int i=,last=;i<=n;i=last+)
- {
- last=min(n/(n/i),m/(m/i));
- res+=(LL)(sum[last]-sum[i-])*(n/i)*(m/i);
- }
- return res;
- }
- int main()
- {
- int T,n,m;
- Mobius();
- T=read();
- while(T--)
- {
- n=read();m=read();
- LL ans=solve(n,m);
- printf("%lld\n",ans);
- }
- }
SPOJ PGCD(莫比乌斯反演)的更多相关文章
- bzoj 2820 / SPOJ PGCD 莫比乌斯反演
那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...
- SPOJ 7001(莫比乌斯反演)
传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...
- SPOJ - VLATTICE (莫比乌斯反演)
Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many latt ...
- SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)
4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)
题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1 a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...
- SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3
http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)
Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...
随机推荐
- android怎样实现自动点击功能
一个按钮之类的控件的自动点击的话,可以定时调用 button.performClick();
- 1030 - Image Is Everything
Your new company is building a robot that can hold small lightweight objects. The robot will have th ...
- android 播放音乐-进度条
今天学渣研究了一下使用MediaPlayer播放音乐时加入进度条,进度条如今用的是android自带的seekbar,后期会跟换UI的,在之前可以播放音乐的基础上,如今加入的主要功能有两个: 1实时显 ...
- 内部框架——axure线框图部件库介绍
网页框架代码<iframe border=0 name=lantk src="要嵌入的网页地址" width=400 height=400 allowTransparency ...
- Storm流计算从入门到精通之技术篇(高并发策略、批处理事务、Trident精解、运维监控、企业场景)
1.Storm全面.系统.深入讲解,采用最新的稳定版本Storm 0.9.0.1 : 2.注重实践,对较抽象难懂的技术点如Grouping策略.并发度及线程安全.批处理事务.DRPC.Storm ...
- SRM 583 Div Level Two:IDNumberVerification
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12610 这道题比较有意思,估计是中国人出的吧,以前都不知道身份 ...
- QT---系统托盘图标不显示原因
很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...
- Deep Learning深入研究整理学习笔记五
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- HBASE完全分布式模式的安装
1集群环境下hadoop.1.1.2已经安装成功 2配置hosts,及环境变量 3编辑hbase-env.xml 4编辑hbase-site.xml 5编辑regionservers文件 6把Hbas ...