Codeforces 803F(容斥原理)】的更多相关文章

题意: 给n个正整数,求有多少个GCD为1的子序列.答案对1e9+7取模. 1<=n<=1e5,数字ai满足1<=ai<=1e5 分析: 设f(x)表示以x为公约数的子序列个数 那么ans=f(1)- Σf(pi) + Σf(pi*pj) - Σf(pi*pj*pk) ........ 注意到对于一个f(x),它对结果的贡献是: 若x为合数,那么前面系数为0 若x由奇数个素数组成,那么系数是-1 若x由偶数个素数组成,那么系数是1 这明显就是莫比乌斯函数……直接一个线性筛出来 那么…
[题目链接]:http://codeforces.com/contest/803/problem/F [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] 计算cnt[x]; 表示数组里有多少个数是x的倍数; 则某个子列里面所有的数字都能被x整除的子列个数为2cnt[x]−1 把这个值记为f[x]; 则我们用需要用容斥原理; 把所有能被2,3,..n整除的子列删除掉; 这里面会有重复计数的问题; 解决方式是,从大到小枚举f[x] 然后对于y=i*x的…
Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[i]: 有多少个数字是 i 的倍数. 所有元素都是1的倍数的序列有:$2^n-1$个.先把$2^n-1$设为答案 所有元素都是质数的倍数的序列有:$\sum 2^{num[p_1]} - 1$个,这些序列不存在的,得从答案中减去. 所有元素都是两质数之积的倍数的序列有:$\sum 2^{num[p_…
原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的集合的个数. 思路:首先知道一个大小为n的集合有2n-1个非空子集,运用容斥,对某个数,我们可以求出它作为因子出现的个数(假设为ki).推一下式子,可以得到结果就等于:Σmiu[i]*(2i-1),其中miu[i]是莫比乌斯函数. 时间复杂度为:O(n*sqrt(max_a)),看起来似乎会超时,实…
http://codeforces.com/problemset/problem/803/F #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define mod 1000000…
$dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <queue> #include <stack> #include <vector>…
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences.…
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int fac[N], cnt; void factor(int n) { cnt = ; int limit = sqrt(n); ; i <= limit; ++i) { ) fa…
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组合. 解题思路:2n的状态,枚举说那些花坛的花取超过了,剩下的用C(n−1sum+n−1)隔板法计算个数.注意奇数的位置要用减的.偶数的位置用加的.容斥原理. #include <cstdio> #include <cstring> #include <cmath> #in…
A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at…
题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Iahub is so happy about inventing bubble sort graphs that he's sta…
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a m…
[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数.要保证每行每列的格子上的数最小值为1,有多少种方案 \(n \leq 250,k \leq 10^9\) 分析 这题有\(O(n^3)\)的dp做法,但个人感觉不如\(O(n^2 \log n)\)直接用数学方法求更好理解. 考虑容斥原理,枚举至少有\(i\)行最小值>1,有\(j\)行最小值>1…
[Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) 分析 显然任意染色的方案数为\(3^{n^2}\),我们考虑求出没有一行一列只有一种颜色的方案数,然后相减. (1)首先考虑仅仅没有全部是一种颜色的列,每一列任意染色有\(3^n\)种方案,去掉每一列只有一种颜色的方案有3种,共\(3^n-3\)种,n列就有\((3^n-3)^n\)种. (2)再…
Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2500 的 D1C,可个人认为难度堪比某些 *2700 *2800. 不过嘛,*2500 终究还是 *2500,还是被我自己想出来了 双倍经验 P4448 [AHOI2018初中组]球球的排列 哦 u1s1 其实一年以前做过 P4448,不过好像直到我 AC 这道题之后才发现这俩题一模一样,并且似乎这次用的方法和上次还不太一样 跑题了跑题了 首先有个性质:\(\forall a,b,c\in\mathbb{N}^+\) 若 \(…
这题又是容斥原理,最近各种做容斥原理啊.当然,好像题解给的不是容斥原理的方法,而是用到Lucas定理好像.这里只讲容斥的做法. 题意:从n个容器中总共取s朵花出来,问有多少种情况.其中告诉你每个盒子中有多少朵花. 分析:其实就是求方程: x1+x2+...+xn = s 的整数解的个数,方程满足: 0<=x1<=a[1], 0<=x2<=a[2]... 设:A1 = {x1 >= a[1]+1} , A2 = {x2 >= a[2]+1} , .... , An = {…
K. Indivisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input output standard output IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following…
D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard input output:standard output Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the res…
传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤1e9,k≤1e6) 思路: 容斥原理套路: 先不考虑是否选全kkk种颜色,方案数为Cmk∗k∗(k−1)n−1C_m^k*k*(k-1)^{n-1}Cmk​∗k∗(k−1)n−1. 然后枚举剩下的至少有几种颜色没选来容斥掉非法情况: 于是Ans=Cmk∑i=k1(−1)k−iCkii(i−1)n−…
[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累加和. [题解] 我们枚举GCD,统计为其倍数的数字数量,先假定其能组成的集合数为贡献, 但是我们发现在统计的过程中有多余统计的部分,比如4和8是2的倍数, 然而它们的GCD等于4,所以我们对于每个集合数的贡献要减去所有其倍数的集合数的贡献, 倒着容斥即可. [代码] #include <cstdi…
B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consis…
问有多少个这样的数字序列 所有数的GCD等于x 并且 所有数的和等于y 题解: 非常难有思路啊 看题解后过的. 考虑序列GCD为x的倍数 即GCD = n*x 和当然都为y 这个条件不要忘了 这样我们可以用  容斥原理来递推的计算GCD为n*x的序列个数是多少 怎么计算呢 以样例为例子 3 9 当GCD = 3 的时候 可以有9 / 3 = 3 个3 序列是这样的 3 3 3 那么有三个空 用插板法 可以计算可以插板的方式数位2**(3-1) = 2**2 = 4种 这里解释插板的意义 3|3…
E. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's…
IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each t…
题目大意: 对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数 分析: 从m种颜色中选出k种,有c(m,k)种方法,那么我们只用考虑 k种颜色的涂法即可 显然第一个物品有k种涂法,后面的因为不能跟前面的相同都只有k-1种涂法 因此容易想到一个公式:k*(k-1)^(n-1) 但是这个公式算的是 不超过k种颜色的涂法,题目要求必须k种,怎么办呢? 先考虑一个简化版的问题: 用而且用完5种颜色涂不相关的五个物品的方案数 用阶乘的方法可以算出…
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C(sum+n-1, n-1)种, 但是这个题, 因为每个盒子里的小球有上限, 所有用刚才那种方法不行. 但是我们可以枚举. n只有20, 一共(1<<20)-1种状态, 每种状态, 1代表取这个盒子里的小球超过了上限, 0代表没有. 一共取sum个, 如果一个盒子里面的小球超过了上限, 那么就还剩下…
题目链接:900D  Unusual Sequences 题意: 给出两个数N,M.让你求数列(和为M,gcd为N)的个数. 题解: 首先,比较容易发现的是M%N如果不为零,那么一定不能构成这样的序列.那么可以设 k = M/N,则可以想象为用k个1来构成序列的个数,运用隔板原理可以求出k个1可以构成的序列总数为2^(k-1),但是这里面其实有不构成条件的(gcd>N)比方说6个相同的数(2,2,2)构成这样gcd就是2×N而不是N了.所以要减去这些数的情况,这样减的话发现不能用递归来做,要先记…
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] 需要用SOSdp #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define…
B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consis…
给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/jaihk662/article/details/77161436. 注意1*C(n,1)+2*C(n,2)+...+n*C(n,n)=n*2^(n-1). #include<cstdio> using namespace std; typedef long long ll; #define MOD…