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 打包 /${zipalign}" error=2, No such file or directory
当我更新完android L proview之后我的打包出问题了,报错/${zipalign}" error=2, No such file or directory 排查了一下午才知道 近 ...
- ASP.NET - URL中参数加密解密操作
效果: 代码: using System; using System.Text; using System.IO; using System.Security.Cryptography; public ...
- Extjs实现树形结构三连选
当项目中需要一个部门人员选择或者省市县地域连选时,就需要树形结构的连选. 再此,写了一个简单的树形结构三连选功能,模拟从后台读取数据和处理数据(欢迎大家交流指正). 代码如下: 循环创建三棵树,其中只 ...
- MFC获取rgb图像数据后动态显示及保存图片的方法
该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw32.lib 链接库 //---------------- ...
- Re-installation failed due to different application signatures.
出现此问题是由于apk的签名不同所致(假设不知道签名是什么 请看上一篇Android应用程序签名 debug签名).假设你是使用的自己的签名,那就是你新版本号的apk使用的签名文件与上一版本号(也就 ...
- leetcode先刷_Path Sum
水的问题不解释,具有参数保持部和,当它到达一个叶子节点,推断是否与给予平等. 需要注意的是节点在树中的数目值它可以是正的或负.它不使用,修剪.有仅仅存在罐.因此,关于或代表最终结果的字. bool h ...
- uva 147 Dollars(完全背包)
题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...
- Ubuntu下装QQ2012,让linux小白们不怕脱离windows
嘿嘿,很多人可能跟我一样,QQ上同学群里会通知一些事项,所以我们希望可以在linux下开QQ,但是QQ官网做的QQ For Linux, 实在是烂的不行 那么怎么在linux下装我们平时在window ...
- CImageList用法介绍
图像列表控制(CImageList)是相同大小图像的一个集合,每个集合中均以0为图像的索引序号基数,图像列表通常由大图标或位图构成,其中包含透明位图模式.可以利用WINDOWS32位应用程序接口函数A ...
- 用VC实现竖写汉字的方法
中国人自古就有自右至左.从上到下书写汉字的习惯.而当我们在自己所编写的应用程序中使用输出函数输出的总是自左至右的横排文字.有没有可能在我们的应用程序中实现竖写汉字的效果呢?笔者偶然发现了一种利用VC实 ...