BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.、
思考:推,。
建立gcd(x,y) = p,然后,x / p与y / p互素
问题就转化成了N / p中有多少个数互质,然后累加就能够了.
=>对于随意a,b,a <= N / p,b <= N / p,且a与b互质
=>gcd(a,b) == 1
如今问题就非常明显了。看到这个形式就非常easy想到欧拉函数。求一下phi,算一下前缀和,累加。
注意这里求欧拉一定要线性的,1qw的数据。nloglogn都非常悬。
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10000010
using namespace std; bool not_prime[MAX];
int prime[MAX],primes;
long long phi[MAX],ans; int n; inline void Eratosthenes(); int main()
{
cin >> n;
Eratosthenes();
for(int i = 2;i <= n; ++i)
phi[i] += phi[i - 1];
for(int i = 1;i <= primes; ++i)
ans += phi[n / prime[i]];
cout << (ans << 1) - primes << endl;
return 0;
} inline void Eratosthenes()
{
phi[1] = 1;
for(int i = 2;i <= n; ++i) {
if(!not_prime[i])
prime[++primes] = i,phi[i] = i - 1;
for(int j = 1;j <= primes && prime[j] * i <= n; ++j) {
not_prime[i * prime[j]] = true;
if(i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)的更多相关文章
- BZOJ 2818 Gcd 线性欧拉
题意:链接 方法:线性欧拉 解析: 首先列一下表达式 gcd(x,y)=z(z是素数而且x,y<=n). 然后我们能够得到什么呢? gcd(x/z,y/z)=1; 最好还是令y>=x 则能 ...
- bzoj 2818 gcd 线性欧拉函数
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1< ...
- bzoj 2818 GCD 数论 欧拉函数
bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...
- BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】
传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec Memory Limit ...
- bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题意] 问(x,y)为质数的有序点对的数目. [思路一] 定义f[i]表示i之 ...
- BZOJ 2818 GCD(欧拉函数)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37161 题意:gcd(x, y) = 质数, 1 <= x, ...
- POJ2909_Goldbach's Conjecture(线性欧拉筛)
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...
- 【BZOJ】2818: Gcd(欧拉函数+质数)
题目 传送门:QWQ 分析 仪仗队 呃,看到题后感觉很像上面的仪仗队. 仪仗队求的是$ gcd(a,b)=1 $ 本题求的是$ gcd(a,b)=m $ 其中m是质数 把 $ gcd(a,b)=1 $ ...
- hdu3572线性欧拉筛
用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...
随机推荐
- Python中可避免读写乱码的一个强慷慨法
昨天在帮同学解析一批从网络上爬取的文件时,遇到一个奇葩的问题,文件本身的编码是gbk,Eclipse编辑环境的默认编码是utf8,使用常规的open方法批量打开文件时,某些文件里存在一些不可被gbk识 ...
- Swift iOS tableView static cell动态计算高度
TableView是iOS开发中经常使用的组件.有些表格由于UILabel包括的文本字数不一样,须要显示的高度也会不同,因此须要动态计算static cell的高度.我用的是static cell,注 ...
- H5移动端IOS/Android兼容性总结,持续更新中…
H5移动端IOS/Android兼容性总结,持续更新中… 1. IOS不识别日期 new Date("2018-07-01 08:00:00")在Android下正常显示可以直接进 ...
- Linear to physical address translation with support for page attributes
Embodiments of the invention are generally directed to systems, methods, and apparatuses for linear ...
- uva_658_It's not a Bug, it's a Feature!(最短路)
It's not a Bug, it's a Feature! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- FastSocket学习笔记~RPC的思想,面向对象的灵活
首先非常感谢这位来自新浪的老兄,它开发的这个FastSocket非常不错,先不说性能如何,单说它的使用方式和理念上就很让人赞口,从宏观上看,它更像是一种远程过程的调用RPC,即服务器公开一些命令,供客 ...
- 一次svn数据库的崩溃错误的解决
作者:朱金灿 来源:. 然后再更新svn数据库,依然出现上面提到的错误.于是又想到新建一个svn数据库,然后将旧库备份出来再导入到新库中,运行命令:svnadmin dump E:\Repositor ...
- flash stm32的flash编写
定义一个全局变量数组:const u8 TEXT_Buffer[]={"STM32F103 FLASH TEST"}; //u8和char* 写入到内存里会有什么区别???? ...
- php curl header头
工作中第一次用到header做个记录 工作中需要在heaer里面加上 Authorization 用来验证身份 public function index() { $url = "http: ...
- css 控制行数 多出的省略
<html lang="en"> <head> <meta charset="UTF-8"> <title>Do ...