题目链接 \(Description\) 给定\(n*m\)的网格,有些格子不能走.求有多少种从\((1,1)\)走到\((n,m)\)的两条不相交路径. \(n,m\leq 3000\). \(Solution\) 容斥,用总方案数减去路径一定相交的方案数. 怎么算呢?注意到两条相交的路径(一定)可以看做从\((1,2)\)到\((n,m-1)\)和从\((2,1)\)到\((n-1,m)\)的两条路径.总方案数也可以看做从\((1,2)\)到\((n-1,m)\)和从\((2,1)\)到\(…
K. Indivisibility 题意:给一个n(1 <= n <= 10^18)的区间,问区间中有多少个数不能被2~10这些数整除: 整除只需要看素数即可,只有2,3,5,7四个素数:基本的容斥原理:数据很小直接用二进制模拟了: int main() { ll n,a[] = {,,,}; scanf("%I64d",&n); ll cnt = n; ;id < (<<);id++){ ; rep0(i,,)<<i)){ num *…
题意:给你n个数,将他们分成连续的三个部分使得每个部分的和相同,求出分法的种数. 思路:用一个数组a[i]记下从第一个点到当前i点的总和.最后一个点是总和为sum的点,只需求出总和为1/3sum的点和总和为2/3sum的点搭配的所有情况.遍历一遍总和为2/3sum的点前总和为1/3sum的点的个数. #include<stdio.h> #include<string.h> #include<iostream> using namespace std; ; typedef…
Jzzhu and Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-20) Description Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence o…
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…
题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选k种颜色有C(m, k)种方案,对于k种颜色方案为k*(k-1)^(n-1)种.但是C(m, k)*k*(k-1)^(n-1)方案包括了选k-1,k-2...,2种方案. 题目要求刚好k种颜色,所以这里想到用容斥. 但是要是直接C(m, k)*k*(k-1)^(n-1) - C(m, k-1)*(k…
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/problem/B Description Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of ex…
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_…
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hen…
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 secondsmemory limit per test256 megabytes 问题描述 Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flow…
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x,b+y,c+z构成三角形,x+y+z<=l,成立的x,y,z有多少种. analyse: 这题在推公式的时候细心一点就没问题了. 基本的思路是容斥:ans=所有的组合情况-不满足条件的情况. 1.求所有的组合情况 方法是找规律: 首先只考虑l全部都用掉的情况. l=1:3 l=2:6 l=3:10…
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watch…
CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(10^9+7\) ) \(solution:\) 首先 \(orz\) 鸽王,看一眼就说:"嗯?这不就是一道格路+容斥的小水题吗?",然后秒切大火题. 这道题主要考验我们如何设置动态规划的状态以保证不重不漏的算好所有情况.上一次我发这类"找基准点"的DP题解应该是POJ…
题目链接: https://codeforces.com/contest/1228/problem/E 题意: 给n*n的矩阵填数,使得每行和每列最小值都是1 矩阵中可以填1到$k$的数 数据范围: $1\leq n \leq 250$ $1\leq k \leq 250$ 分析: 参考博客:https://www.cnblogs.com/scx2015noip-as-php/p/cf589e.html 先预处理出f(x)代表x*n的矩阵,每列最小值是1的填充方案数 以下讨论的方案数,列的最小值…
G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest…
题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b 的实数对个数. 题解:看到求区间内满足一定条件的数的个数,应该用数位dp,数位dp基本操作是编写出solve函数调用记忆化搜索,那么考虑solve(R,R)是求0到R满足条件的答案,solve(L-1,R)求a属于0到L-1,b属于0到R满足条件的答案,solve(L-1,L-1)是ab都属于0到L…
Codeforces 题面传送门 & 洛谷题面传送门 u1s1 感觉这道题放到 D1+D2 里作为 5250 分的 I 有点偏简单了吧 首先一件非常显然的事情是,如果我们已知了排列对应的阶梯序列,那么排列中每个极长的连续阶梯段就已经确定了,具体来说,由于显然极大的连续段之间不能相交,因此假设 \(a\) 为 \(p\) 的阶梯序列,对于 \(a\) 数组中每个值相同的极大连续段 \([l,r]\),显然我们只能每 \(a_l\) 个元素将其划分成 \([l,l+a_l-1],[l+a_l,l+2…
题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数 s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数 整除的个数! 那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! #includ…
"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容斥一下, 复杂度O(n^2+q) #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmat…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more import…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3 ×…
Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pa…
题目链接 \(Description\) 给定\(n\)个正整数\(a_i\).求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2},...,a_{i_k}\) \(and\)起来为\(0\). \(n\leq10^6,\quad 0\leq a_i\leq10^6\). \(Solution\) 这个数据范围..考虑按位容斥: 令\(g_x\)表示\(x\)的二进制表示中\(1\)的个数,\(f_x\)表示有多少个\(a_i\)…
题目链接 那场完整的Div2(Div1 ABC)在这儿.. \(Description\) 给定\(n(n\leq 10^6)\),用三种颜色染有\(n\times n\)个格子的矩形,求至少有一行或一列格子同色的方案数. \(Solution\) 求恰好有多少行/列满足同色不好求,但如果某几行/列已经确定同色,这些行/列外任意选择,即至少多少行/列满足,那么很好求. 容斥.设\(f(i,j)\)表示至少有\(i\)行\(j\)列同色的方案数,则\(ans=\sum_{0\leq i\leq n…
大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其中$\alpha$为$x$二进制中$1$的个数, $f_x$表示与和等于$x$的非空子集数. $f_x$是一个$20$维前缀和, 按传统容斥做法的话显然要超时, 可以每次求一维的和, 再累加 比方说对于2维前缀和, 用容斥的求法是这样 for (int i=1; i<=n; ++i) { for (in…
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation…
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的方案(其实也是容斥),或者可以直接枚举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个不…
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.…
题目链接 题意 给定 \(x,p,k\),求大于 \(x\) 的第 \(k\) 个与 \(p\) 互质的数. 思路 参考 蒟蒻JHY. 二分答案 \(y\),再去 \(check\) 在 \([x,y]\) 区间中是否有 \(k\) 个与 \(p\) 互质的数. \(check\) 采用容斥,将 \(p\) 质因数分解,用这些质数组合成的数在 \([1,y]\) 范围内 容斥. Code #include <bits/stdc++.h> using namespace std; typedef…