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: ...
随机推荐
- hdoj2680 Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 深度学习笔记之使用Faster-Rcnn进行目标检测 (实践篇)
实验 我使用的代码是Python版本的Faster Rcnn,官方也有Matlab版本的,链接如下: py-faster-rcnn(python) faster-rcnn(matlab) 环境配置 按 ...
- VTMagic的使用
// VTMagic的使用 // CFOrderViewController.m // qifuyuniOS //// /** * @author 李洪强, 16-08-30 10:08:50 ...
- NHibernate查询导致Update问题
用NHibernate,总感觉怪事多罗罗. 比如说,明明我们是在查询,却报错,刨根问底找到出错原因,竟然是因为执行了一些Update甚至Insert!老天,我们明明只是查询而已,什么时候有更新过数据? ...
- Flame Graphs
http://www.brendangregg.com/flamegraphs.html Flame graphs are a visualization of profiled software, ...
- return value, output parameter,
Return Value https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=s ...
- MARGIN-BEFORE MARGIN-AFTER MARGIN-START MARGIN-END
总的来说:这是CSS3.0的对于文章段P容器的定义方法语句!display:block这个样式,只定义了P容器为一个块;后面四句是CSS3中的样式定义方法:-webkit-margin-before: ...
- js基础用法1
click() 对象.click() 使对象被点击.closed 对象.closed 对象窗口是否已封闭true/falseclearTimeout(对象) 清除已设置的setTimeout对象cle ...
- POJ - 3468 A Simple Problem with Integers(线段树区间更新,区间查询)
1.给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). "Q a b" ...
- 我为什么要学习C++反汇编
写在开始 从6月7日开始到今天已经有5天了,在这5天的业余时间(工作之余)里终于系统的完成了C++反汇编的大部分问题的学习,今天写篇总结,算是对这几天学习的总结. 首先我想说明的一个问题就是我为什么要 ...