Coprime Conundrum 容斥原理
https://www.hackerrank.com/contests/hourrank-13/challenges/arthur-and-coprimes
我们可以枚举每一个p在[2, sqrt(n)]里,然后就是在[p + 1, n / p]中找有多少个数和p互质了。
标准容斥,先算出[1, n / p]中有多少个和p互质,这个是不能用欧拉定理做的,需要把p质因数分解,然后dfs
求解元素X在区间[1, up]中,有多少个数和X互质。(容斥)
思路:把X质因数分解,多了的不要。12 = 2 * 3。然后有个明显的道理就是如果是2的倍数的话,那么就一定不会与12互质,所以需要减去2的倍数,减去3的倍数,再加上6的倍数。容斥的思路好想,但是不怎么好写。所以结果是总数量up – 不互质的个数。
预处理;his[val][]表示元素val拥有的质因子,Size[val]表示有多少个。记得1是不做任何处理的。就是Size[1] = 0。Dfs的cur表示下一次从哪里开始,不往回枚举,就是任意k个值。
int calc(int up, int cur, int number, int tobuild, int flag) { //一开始flag是0。0表示加,1减
int ans = 0;
for (int i = cur; i <= Size[number]; ++i) {
if (flag == 0) {
ans += up / (his[number][i] * tobuild);
} else ans -= up / (his[number][i] * tobuild);
ans += calc(up, i + 1, number, his[number][i] * tobuild, !flag);
}
return ans;
}
计算12在[1, 24]就是24 - calc(24, 1, 12, 1, 0)。tobuild是选择k个质因数后生成的数字。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int prime[maxn];
bool check[maxn];
int total;
int Size[maxn];
int his[maxn][ + ];
void initprime() {
for (int i = ; i <= maxn - ; i++) {
if (!check[i]) {
prime[++total] = i;
}
for (int j = ; j <= total; j++) {
if (i * prime[j] > maxn - ) break;
check[i * prime[j]] = ;
if (i % prime[j] == ) break;
}
}
for (int i = ; i <= maxn - ; ++i) {
int t = i;
for (int j = ; j <= total; ++j) {
if (prime[j] > t) break;
if (t % prime[j] == ) {
his[i][++Size[i]] = prime[j];
while (t % prime[j]) {
t /= prime[j];
}
}
}
}
return ;
}
LL calc(int up, int cur, int number, int tobuild, int flag) {
LL ans = ;
for (int i = cur; i <= Size[number]; ++i) {
if (flag == ) {
ans += up / (his[number][i] * tobuild);
} else ans -= up / (his[number][i] * tobuild);
ans += calc(up, i + , number, his[number][i] * tobuild, !flag);
}
return ans;
}
void work() {
int n;
cin >> n;
int en = (int)sqrt(n + 0.5);
// cout << en << endl;
LL ans = ;
for (int i = ; i <= en; ++i) {
ans += n / i - calc(n / i, , i, , );
ans -= i - calc(i, , i, , );
// cout << calc(n / i, 1, i, 1, 0) << " " << calc(i, 1, i, 1, 0) << endl;
// cout << ans << endl;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
initprime();
work();
return ;
}

Coprime Conundrum 容斥原理的更多相关文章
- HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU4135 Co-prime(容斥原理)
题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
- hdu4135 Co-prime【容斥原理】
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Co-prime(容斥原理)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- [容斥原理] hdu 4135 Co-prime
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...
- hdu4135 Co-prime 容斥原理
Given a number N, you are asked to count the number of integers between A and B inclusive which are ...
- HDU 5072 Coprime (单色三角形+容斥原理)
题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- [wxWidgets]_[0基础]_[不常见但有用的类wxCmdLineParser]
场景: 1. 有时候须要构造命令行字符串传递給函数调用,比方CreateProcess,假设參数是动态的,那么就得使用类似std::vector<string>加入单个參数,之后拼接为一个 ...
- C++第9周(春)项目5 - 一元一次方程类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...
- hdu4183往返经过至多每一个点一次/最大流
题意:从s到t,每一个点有f值,仅仅能从f值小的到大的.到T后回来.仅仅能从f值大的到 小的,求可行否. 往返,事实上就是俩条路过去(每一个点最多一次).所以想到流量为2,跑最大流.看是否满2,又要每 ...
- Python爬虫开发【第1篇】【机器视觉及Tesseract】
ORC库概述 在读取和处理图像.图像相关的机器学习以及创建图像等任务中,Python 一直都是非常出色的语言.虽然有很多库可以进行图像处理,但在这里我们只重点介绍:Tesseract 1.Tesser ...
- scrapy框架的解析
1,scrapy框架的官网:https://scrapy.org/ 什么是scrapy框架: scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用内框架,非常出名,所谓框架就是一个已经继承 ...
- Django的各种初识
1,django项目的各个文件的介绍 1.1>项目的根目录:是各个子文件的根目录,在各个文件相互导入文件的时候使用 1.2>配置文件:为django的各个文件配置相关的各种默认配置 1.3 ...
- Codevs 2006=BZOJ 2964 Boss单挑战
2964: Boss单挑战 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 266 Solved: 120[Submit][Status][Discu ...
- JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...
- html页面缓存问题
若IIS没有设置,html页面一旦缓存,则永远缓存. Chrome如下 火狐如下 一种方法:我们一般通过xxx.html?20151010这样URL欺骗浏览器. 另一种方法:设置IIS,让永远客户端不 ...
- YTU 2920: Shape系列-7
2921: Shape系列-7 时间限制: 1 Sec 内存限制: 128 MB 提交: 156 解决: 129 题目描述 小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函 ...