gcd(x,y)(1<=x,y<=n)为素数(暂且把(x,y)和(y,x)算一种) 的个数

<=> gcd(x/k,y/k)=1,k是x的质因数 的个数

<=> Σφ(x/k) (1<=x<=n,k是x的质因子)

这样的复杂度无法接受,

∴我们可以考虑枚举k,计算Σφ(q/k) (k是n以内的质数,q是n以内k的倍数),即Σ[φ(1)+φ(2)+φ(3)+...+φ(p)] (p=n/k)

介个phi的前缀和可以预处理粗来。

但是(x,y)和(y,x)并不同,所以在计算前缀和的时候,对于φ(x) (x≠1),要乘2再累加,即Σ[φ(1)+φ(2)*2+φ(3)*2+...+φ(p)*2] (p=n/k)。

∴对每个n以内的素数,我们可以O(1)地得到其对答案的贡献。

∴时间复杂度花费在筛素数和预处理phi上,为O(n*log(log(n)))或O(n)[线性筛]。

 #include<cstdio>
using namespace std;
typedef long long ll;
int phi[],n;
bool unPrime[];
ll ans,sum[];
void Shai_Prime()
{
unPrime[]=;
for(ll i=;i<=n;i++) if(!unPrime[i])
{
ans+=sum[n/i];
for(ll j=i*i;j<=n;j+=i)
unPrime[j]=;
}
}
void phi_table()
{
phi[]=;//规定phi(1)=1;
for(int i=;i<=n;i++)
if(!phi[i])//若i是质数(类似筛法的思想)
for(int j=i;j<=n;j+=i)//i一定是j的质因数
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
void init_sum()
{
sum[]=phi[];
for(int i=;i<=n;i++) sum[i]=(ll)(phi[i]<<)+sum[i-];
}
int main()
{
scanf("%d",&n); phi_table(); init_sum(); Shai_Prime();
printf("%lld\n",ans);
return ;
}

【数论】【筛法求素数】【欧拉函数】bzoj2818 Gcd的更多相关文章

  1. √n求单值欧拉函数

    基本定理: 首先看一下核心代码: 核心代码 原理解析: 当初我看不懂这段代码,主要有这么几个问题: 1.定理里面不是一开始写了一个n*xxx么?为什么代码里没有*n? 2.ans不是*(prime[i ...

  2. 素数&欧拉函数

    素数表 const int maxN找[1,maxN)内的素数 int prime[int I]第I个素数 const int maxN=1e5+5; int prime[maxN]; bool ma ...

  3. 求逆欧拉函数(arc)

    已知欧拉函数计算公式 初始公式:φ(n)=n*(1-1/p1)*(1-1/p2).....*(1-1/pm)   又 n=p1^a1*p2^a2*...*ps^as  欧拉函数是积性函数 那么:φ(n ...

  4. (数论)51NOD 1136 欧拉函数

    对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...

  5. hdu (欧拉函数+容斥原理) GCD

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...

  6. Longge's problem poj2480 欧拉函数,gcd

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6918   Accepted: 2234 ...

  7. GCD - Extreme (II) UVA - 11426 欧拉函数与gcd

    题目大意: 累加从1到n,任意两个数的gcd(i,j)(1=<i<n&&i<j<=n). 题解:假设a<b,如果gcd(a,b)=c.则gcd(a/c,b ...

  8. Bzoj-2818 Gcd 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...

  9. O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求

    筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...

  10. BZOJ 2818 GCD 素数筛+欧拉函数+前缀和

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=n且Gcd(x,y)为素数的数对( ...

随机推荐

  1. 2017年研究生数学建模D题(前景目标检测)相关论文与实验结果

    一直都想参加下数学建模,通过几个月培训学到一些好的数学思想和方法,今年终于有时间有机会有队友一起参加了研究生数模,but,为啥今年说不培训直接参加国赛,泪目~_~~,然后比赛前也基本没看,直接硬刚.比 ...

  2. [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)

    [Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...

  3. 设置edittext的样式

    1.在res->drawable编写 <?xml version="1.0" encoding="utf-8"?> <shape xml ...

  4. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  5. 如何在plsql/developer的命令窗口执行sql脚本

    在plsql/developer的命令窗口执行sql脚本的命令是@+路径 示例如下: 第一步:在C:\Users\linsenq\Desktop目录下新建一个脚本文件: test.sql test.s ...

  6. bzoj 3456 城市规划 多项式求逆+分治FFT

    城市规划 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1091  Solved: 629[Submit][Status][Discuss] Desc ...

  7. poj3580 序列之王 fhqtreap

    fhqtreap的写法 操作其实都差不多哇 #include<cstdio> #include<cstring> #include<algorithm> using ...

  8. 使用dd命令克隆整个系统

    神奇的ghost的原理是什么呢?不就是数据复制吗?Linux下的dd命令不就是最强大的数据复制工具!          既然如此,我为什么要使用g4l这样复杂的工具呢?一条dd命令不就可以帮我实现任意 ...

  9. Django-views,用户认证,login_requierd()

    分别是认证,登入,注销的功能 authenticated():验证是否登录 user = authenticate(username='someone',password='somepassword' ...

  10. struts标签include传参的问题

    传一个常量过去居然为null, <s:include value="/biz/customer/corp/module/franchisemanageright/corpFranchi ...