2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1633  Solved: 724
[Submit][Status]

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

Source

求gcd(x,y)==prime[k] 對數(1<=x,y<=n)

枚舉質數p,求gcd(x,y)==1, (1<=x,y<=n/p)

設sphi(k)表示gcd(x,y)==1,(1<=x,y<=k),那麼,可以通過幾何法推導出sphi(k)=phi(k)*2+sphi(k-1)

然後此題可解。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 11000000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
} int n,m;
bool pflag[MAXN];
int prime[MAXN],topp=-;;
int phi[MAXN];
void init()
{
int i,j,k;
int x,y;
phi[]=;
for (i=;i<MAXN;i++)
{
if (!pflag[i])
{
prime[++topp]=i;
phi[i]=i-;
}
for (j=;j<=topp && i*prime[j]<MAXN;j++)
{
pflag[i*prime[j]]=true;
phi[i*prime[j]]=phi[i]*phi[prime[j]];
if (i%prime[j]==)
{
x=i;y=prime[j];
k=;
while (x%prime[j]==)
{
x/=prime[j];
y*=prime[j];
k++;
}
if (x==)
{
phi[i*prime[j]]=y-y/prime[j];
}else
{
phi[i*prime[j]]=phi[x]*phi[y];
}
break;
}
}
}
}
qword sphi[MAXN];
int main()
{
freopen("input.txt","r",stdin);
int i,j,k;
init();
scanf("%d",&n);
qword ans=;
sphi[]=;
for (i=;i<=n;i++)
sphi[i]=sphi[i-]+phi[i]*;
for (i=;i<=topp && prime[i]<=n;i++)
{
ans+=sphi[n/prime[i]];
}
printf(LL"\n",ans);
}

bzoj 2818: Gcd 歐拉函數的更多相关文章

  1. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  2. BZOJ 2818 Gcd(欧拉函数+质数筛选)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 9108  Solved: 4066 [Submit][Status][Discu ...

  3. bzoj 2705: [SDOI2012]Longge的问题 歐拉函數

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1035  Solved: 669[Submit][S ...

  4. Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3241  Solved: 1437[Submit][Status][Discuss ...

  5. BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec  Memory Limit ...

  6. BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 2534  Solved: 1129 [Submit][Status][Discu ...

  7. bzoj 2818 gcd 线性欧拉函数

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1< ...

  8. BZOJ 2818: Gcd

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4443  Solved: 1960[Submit][Status][Discuss ...

  9. bzoj 2818: Gcd GCD(a,b) = 素数

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1566  Solved: 691[Submit][Status] Descript ...

随机推荐

  1. [Javascript + rxjs] Simple drag and drop with Observables

    Armed with the map and concatAll functions, we can create fairly complex interactions in a simple wa ...

  2. UVA - 10239 The Book-shelver&#39;s Problem

    Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time ...

  3. mybatis12 Usermapper.xml

    输入和输出映射 通过parameterType完成输入映射,通过resultType和resultMap完成输出映射. 1.1parameterType传递pojo包装对象 可以定义pojo包装类型扩 ...

  4. GitHub与Versions

    [第一步]建立先仓库 第一步的话看一般的提示就知道了,在github新建一个repository(谷歌可以解决),都是可视化的界面操作,所以难度不大.或者看这里:https://help.github ...

  5. iOS创建界面方法的讨论

    以前在入门的时候,找的入门书籍上编写的 demo 都是基于 Storyboards 拖界面的.后来接触公司项目,发现界面都是用纯代码去写复杂的 autoLayout 的.再然后,领导给我发了个 Mas ...

  6. dbartisan下载地址

    http://downloads.embarcadero.com/free/dbartisan

  7. 使用httpModule做权限系统

    页面请求过程: 根据这个流程,网上一般的权限验证在:Http.Module.AuthorizeRequestHttp.Module.PreRequestHandlerExecute 例如使用前者: u ...

  8. java 百分比显示

    DecimalFormat percent = new DecimalFormat("0.00%"); completed_num = (double) involvedTask_ ...

  9. 绝对定位的DIV绝对居中显示

    绝对居中:即在客户端上任何分辨率下纵横方向均居中 紫色的正方形为绝对定位的div position:absolute; top: 50%; left: 50%; 只能把div定位在以红色圈为起点的位置 ...

  10. .NET设计模式(6):原型模式(Prototype Pattern)

    ):原型模式(Prototype Pattern)    );         //使用颜色         string colorName = "red";         C ...