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 ...
随机推荐
- [React] Theme your application with styled-components and "ThemeProvider"
In this styled-components lesson, we set a "primary color" within a UI "theme" o ...
- 《TCP/IP具体解释卷2:实现》笔记--协议控制块
协议层使用协议控制块(PCB)存放各UDP和TCP插口所要求的多个信息片.Internet协议维护Internet协议控制块 (internet protocol control block)和TCP ...
- Something-Summary
1.Combinatorial Mathematics 1.1 Bell Number: \(B_n\)表示元素个数为n的集合划分成若干个不相交集合的方案数. \(B_{n + 1} = \sum_{ ...
- StringBuilder和String的区别
使用 StringBuilder 语言 C# String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...
- JS实现按下按键触发点击事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- linux 内核源代码目录结构
/arch:目录包括了所有和体系结构相关的核心代码.它下面的每一个子目录都代表一种Linux支持的体系结构,例如i386就是Intel CPU及与之相兼容体系结构的子目录.PC机一般都基于此目录. / ...
- [RxJS] BehaviorSubject: representing a value over time
When an Observer subscribe to a BehaviorSubject. It receivces the last emitted value and then all th ...
- Activity生命周期的回调,你应该知道得很多其它!--Android源代码剖析(下)
转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/46910811[yalinfendou的博客] ...
- Hbase常见异常 分类: B7_HBASE 2015-02-02 16:16 412人阅读 评论(0) 收藏
1. HBase is able to connect to ZooKeeper but the connection closes immediately hbase(main):001:0> ...
- Android屏幕信息获取
Android中有时需要获取屏幕的size信息以便对控件位置进行动态控制,最近做了一些研究,现在将获取屏幕大小信息的方法总结如下,可能存在一些地方理解的不全面. 1.getMetrics Displa ...