题意:链接

方法:线性欧拉

解析:

首先列一下表达式

gcd(x,y)=z(z是素数而且x,y<=n).

然后我们能够得到什么呢?

gcd(x/z,y/z)=1;

最好还是令y>=x

则能够得到我们要的答案就是∑max(y/z)i=1phi(i)

而max(y/z)就是max(n/z)。

所以仅仅须要枚举一下质数z随便搞一下就好了,最好用前缀和记录

HINT:前缀和写树状数组的都是(*)

代码:

正常人做法1.1s

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 10000010
using namespace std;
typedef long long ll;
int n,tot;
int prime[N];
int phi[N];
ll sum[N];
int f[N];
void sieve()
{
phi[1]=1;
sum[1]=1;
for(int i=2;i<=n;i++)
{
if(!f[i])
{
prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot,i*prime[j]<=n;j++)
{
f[i*prime[j]]=1;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}else
{
phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
sum[i]=sum[i-1]+phi[i];
}
}
int main()
{
scanf("%d",&n);
sieve();
ll print=0;
for(int i=1;i<=tot;i++)
{
int up=n/prime[i];
print+=sum[up];
}
printf("%lld\n",print*2-tot);
}

树状数组4.8s

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 10000010
using namespace std;
typedef long long ll;
int n,tot;
int prime[N];
int phi[N];
ll sum[N];
int f[N];
int lowbit(int x){return x&(-x);}
void update(int x,int p)
{
while(x<=n)
{
sum[x]+=p;
x+=lowbit(x);
}
}
ll getsum(int x)
{
ll ret=0;
while(x)
{
ret+=sum[x];
x-=lowbit(x);
}
return ret;
}
void sieve()
{
phi[1]=1;
update(1,1);
for(int i=2;i<=n;i++)
{
if(!f[i])
{
prime[++tot]=i;
phi[i]=i-1;
update(i,phi[i]);
}
for(int j=1;j<=tot,i*prime[j]<=n;j++)
{
f[i*prime[j]]=1;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
update(i*prime[j],phi[i*prime[j]]);
break;
}else
{
phi[i*prime[j]]=phi[i]*phi[prime[j]];
update(i*prime[j],phi[i*prime[j]]);
}
}
}
}
int main()
{
scanf("%d",&n);
sieve();
ll print=0;
for(int i=1;i<=tot;i++)
{
int up=n/prime[i];
print+=getsum(up);
}
printf("%lld\n",print*2-tot);
}

BZOJ 2818 Gcd 线性欧拉的更多相关文章

  1. BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...

  2. bzoj 2818 gcd 线性欧拉函数

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

  3. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

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

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

  5. bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题意] 问(x,y)为质数的有序点对的数目. [思路一] 定义f[i]表示i之 ...

  6. BZOJ 2818 GCD(欧拉函数)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37161 题意:gcd(x, y) = 质数, 1 <= x, ...

  7. 【BZOJ】2818: Gcd(欧拉函数+质数)

    题目 传送门:QWQ 分析 仪仗队 呃,看到题后感觉很像上面的仪仗队. 仪仗队求的是$ gcd(a,b)=1 $ 本题求的是$ gcd(a,b)=m $ 其中m是质数 把 $ gcd(a,b)=1 $ ...

  8. HYSBZ 2818 Gcd【欧拉函数/莫比乌斯】

    I - Gcd HYSBZ - 2818 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample In ...

  9. 【BZOJ】2818: Gcd(欧拉函数/莫比乌斯)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2818 我很sb的丢了原来做的一题上去.. 其实这题可以更简单.. 设 $$f[i]=1+2 \tim ...

随机推荐

  1. python自动化测试报告(excel篇)

    转:https://www.jianshu.com/p/6c5d328bf390 # -*- coding: utf-8 -*- import xlsxwriter def get_format(wd ...

  2. java 自定义log类

    目录机构如下: package tpf.common; import org.apache.log4j.*; import java.io.File; import java.net.URL; pub ...

  3. 【CF559C】 Gerald and Giant Chess(计数,方案数DP,数论)

    题意:给出一个棋盘为h*w,现在要从(1,1)到(h,w),其中有n个黑点不能走,问有多少种可能从左上到右下 (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000),答案模10^9+7 思路:从 ...

  4. 转 Python常见数据结构整理

    http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...

  5. android 设置app root权限简单方法

    vim frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java +709 private static void ...

  6. 禁用VMware的vmem文件

    新建一个虚拟机,VMWare会默认为其创建一个虚拟内存文件*.VMEM, 这个文件会影响系统的磁盘性能,所以最好关闭它. 该当是找到*.vmx文件,在文件最后加入一行 mainMem.useNamed ...

  7. LeetCode OJ--Next Permutation *

    求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector&g ...

  8. 你也可以当面霸-MVC的原理及特点

    MVC是面试中经常被问到问题,如果能把MVC的原理简单清楚的描述出来,肯定会在面试官的心目中加分. 如果在能画图的情况下,画出一张MVC的流程图,无疑能简化不少概念上的术语,如果不能也不要紧,只要把核 ...

  9. 洛谷—— P1873 砍树

    https://www.luogu.org/problemnew/show/P1873 题目描述 伐木工人米尔科需要砍倒M米长的木材.这是一个对米尔科来说很容易的工作,因为他有一个漂亮的新伐木机,可以 ...

  10. Codeforces Gym 100733I The Cool Monkeys 拆点+最大流

    原题链接:http://codeforces.com/gym/100733/problem/I 题意 有两颗树(只是树,不是数据结构),每棵树上有不同高度的树枝,然后有m只猴子在某棵树的前m高的树枝上 ...