AtCoder-abc230_g GCD Permutation 容斥】的更多相关文章

Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], ..., a[n]. How many pairs of integers (i, j) satisfy 1 ≤ i ≤ j ≤ n and gcd(i, j) = gcd(a[i], a[j]) = 1? Here gcd means greatest common divisor. Input F…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y…
GCD Expectation Time Limit: 4 Seconds     Memory Limit: 262144 KB Edward has a set of n integers {a1,a2,...,an}. He randomly picks a nonempty subset {x1,x2,-,xm} (each nonempty subset has equal probability to be picked), and would like to know the ex…
题解:这个题目看着挺吓人的,如果仔细想想的话,应该能想出来.题解还是挺好的理解的. 首先设gcd(a1,a2,a3...an)=i,那么a1~an一定是i的倍数,所以ai一共有k/i种取值.有n个数,所有就有(k/i)^n种情况.光是这样还是不行的,因为a1~an的gcd可能为j*i.所以我们要减去2*i,3*i......j*i<=k.倒着来就可以了,复杂度应该是o(nlogn) code: #include<bits/stdc++.h> using namespace std; ty…
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } /********************************************************************/ ; vector<…
好题!学习了好多 写法①: 先求出gcd不为1的集合的数量,显然我们可以从大到小枚举计算每种gcd的方案(其实也是容斥),或者可以直接枚举gcd然后容斥(比如最大值是6就用2^cnt[2]-1+3^cnt[3]-1-(6^cnt[6]-1),cnt[x]表示x的倍数的个数),用容斥计算的话可以发现系数是莫比乌斯函数的相反数,就可以线性筛了.下面会记录一种O(MAX*ln(MAX))的筛法...求cnt的话可以选择直接枚举倍数计算O(MAX*ln(MAX))或者分解质因数,因为1e7内最多有8个不…
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be…
题目链接 求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数. 问题等价于$ x\in[1, a/k] , y \in [1, b/k] $ 内 \(gcd(x, y) = 1\) 的(x, y)的对数. 假设a < b, 那么[1, a/k]这部分可以用欧拉函数算. 设 \(i\in (a/k, b/k]\), (a/k, b/k]这部分可以用容斥算, 用a/k减去[1, a/k]里面和i不互质的数的个数. 具体看代码.…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17385    Accepted Submission(s): 6699 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y…
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个gcd产生的个数,这里要使用容斥定理,f[i]表示的是 gcd 是 i 的个数,g[i] 表示的是 gcd 是 i 倍数的,f[i] = g[i] - f[j] (i|j). 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu…