题目链接: http://codeforces.com/contest/1257/problem/F 题意: 给出$n$个30位整数 找到一个数,让它与这$n$个数分别异或,得到的$n$个数二进制1的个数相同 数据范围: $1\leq n \leq 100$ 分析: CF官方题解称这是中间相遇技巧 枚举答案的低15位,这时有$2^{15}$种情况 与给出的$n$个数的低15位去异或,得到1的数量定义为$low[i]$ 把$(low[2]-low[1],low[3]-low[1],.....low…
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \(n\) 序列 \(A\),和 \(q\) 次询问,对于每一次询问给出两个数 \(l, x\) ,你需要计算在前缀和 \(A[1, l]\) 中选取若干个数,使得它们 \(xor\) 起来的结果等于 \(x\) 的方案数 $n , q \leq 10^5   0 \leq A_i \leq 2^{20} $…
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗树,并最小化删掉环边后的树的直径. \(n \leq 2 \times 10^5\) 树的边权 $ \leq 10^9 $ 解题思路 : 考虑最终树的直径可能由两部分组成,答案是其中的最大值 第一种就是外向树内的直径的最大值,这个只需要随便\(dp\)一下即可,这里不过多讨论 第二种情况树的直径经过原来的环,…
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和,用于之后快速求和.将原数组排序后去重,枚举每一个数做lead的情况下,其余数减少后再求和的结果.不断维护最大值即可. PS:这里用到了一个方便的函数unique()来去重,使用前需要先将数组排序,参数为数组的首地址和尾后地址,返回新的尾后地址.(该函数没有将重复的元素删除,只是放到了尾地址后面)可…
题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<<15,用map匹配. 代码: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <vector> #include &…
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 seconds memory limit per test       256 megabytes input standard input output standard output One day Masha came home and noticed n mice in the corri…
\(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i=1}^{n} i^k\) 对 \(10^9 +7\) 取模的值 \(1 \leq n \leq 10^9, 0 \leq k \leq 10^6\) 解题思路 : 考虑 \(k\) 比较大,且模数不太好 \(NTT\),于是考虑插值做法, 学习了一波插值之后,发现就是个板子题 (雾) 拉格朗日插值: 对于…
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ,一共有 \(q\) 次操作,每次选择一个节点,新建两个节点 \(n + 1, n + 2\) 向其连边,每次操作完之后求树的直径的长度 $1 \leq q \leq 5 \times 10^5 $ 解题思路 : 观察发现,每次加点后,树的直径的端点至多只会变化一个 证明:首先显然新的直径的端点不可能是两个…
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 \(k\) 叉树的形式 \((k \in [1, n - 1])\) . 对于编号为 \(i\) 的点,它的儿子区间是 \([\ k(i-1)+2, \ \min(ki + 1, n)\ ]\) 如果说一个点违反堆性质,当且仅当它的点权小于它父亲的点权,对于所有 \((k \in [1, n - 1])\)…
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w…
题目链接:http://codeforces.com/contest/618/problem/F 题目: 题目大意: 有两个大小为 N 的可重集 A, B, 每个元素都在 1 到 N 之间. 分别找出 A 和 B 的一个子集, 使得这两个子集元素之和相等. 分别输出集合A和集合B的子集的个数以及子集中元素在原集合中的位置 N ≤ $10^6$ 题解: 首先我们证明一个结论,存在一组方案,满足两个子集在A中和在B中都是连续的一段 把两个集合看成两个数组,分别计算出前缀和sa,sb 对于每个i(0<…
题目链接:http://codeforces.com/contest/876/problem/F 题解:一道简单的思维题,知道最多一共有n*(n+1)/2种组合,不用直接找答案直接用总的组合数减去不符合的也行.找不符合的就简单了.找到一个位置i,他的最左边的位置就是a[i]在二进制下是0的最靠近i的位置,所以可以先用pos[j]记录第j位是0的位置.然后最右边的也是同理.这里还处理一下a[l]=a[r]的情况如果这个相同会出现重复考虑所以可以在第一遍找最左边位置的时候做一下处理最左边的位置起码大…
F - Expected Square Beauty 思路:https://codeforces.com/blog/entry/68111 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define…
题  OvO http://codeforces.com/contest/884/problem/F (Educational Codeforces Round 31 - F) 884f 解 题目标签上的 flows 极大降低了难度…… 做法: 首先贪心,每个对应的元素对,固定b值比较大的那个元素设为不交换的元素. 然后费用流,2n+2个点,设源点为0,汇点为2n+1 源点向 1 ~ n 的点连一条费用为0,流量为1的边 从 n+1 ~ 2n 向汇点连一条费用为0,流量为1的边 对于每个固定元素…
题目连接:http://codeforces.com/contest/1263/problem/F 题意:有n个设备,上和下分别连接着一颗树,上下两棵树每棵树的叶子节点连接一个设备,两棵树的根节点都是1,1是源点可以发电供给叶结点连接的设备,现在问最多删除多少条边可以保证从根结点1发电后仍然可以使得所有设备都有电? 如上图删除红色的边(5条)仍然可以保证所有设备能供电 思路: 因为是有上下两棵树,所以对于一个设备可以供电,那么删除上面的树一部分边,要么删除下面一部分边,根据题意,我们当然要删除最…
题目链接: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…
Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslant 2 \times 10^5\) Sol DP+二分. 发现这个东西有后效性,就是前面选不选会影响后面的决策,并且权值太大无法记录. 但是我们可以倒着做,因为后面的决策无法影响前面的决策. \(f[i][j]\) 表示到 \(i\) 删掉 \(j\) 个至少需要的初始权值. 因为初始权值非负,所以…
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n)…
F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output output.txt A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following pro…
F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated them from 11 and nn and then put them into the cage. The cage consists of one row of nn cells, enumerated with integers from 11 to nn from left to right.…
F - Ivan and Burgers 思路:线性基+贪心,保存线性基中每一位的最后一个 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#d…
F - Make It One 思路: dp + 容斥 首先, 答案不会超过7, 因为前7个质数的乘积大于3e5(最坏的情况是7个数, 每个数都缺少一个不同的因子) 所以从1到7依次考虑 dp[i][j]: 表示选取i个数且gcd==j的方案数 dp[i][j] = C(cntj, i) - ∑dp[i][k] (其中cntj表示ai中是j的倍数的个数, k表示所有j的倍数) 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma…
F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l,…
F - Dominant Indices 思路:树上启发式合并 先跑轻子树,然后清除轻子树的信息 最后跑重子树,不清除信息 然后再跑一遍轻子树,重新加回轻子树的信息 由于一个节点到根节点最多有logn个轻边,所以复杂度为nlogn 代码: #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#def…
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所有前驱(在保证最短路径不变的情况下),即找到v,使得dis[v] + 1 == dis[u],并把u和v所连边保存下来 最后就是dfs递归暴力枚举每个点的前驱,然后输出答案 #include<bits/stdc++.h> using namespace std; #define fi first…
F - AND Graph 思路: 首先,x & (~x) == 0 其次,~x 的 子集 y = ((~x) ^ (1<<k)), 0<= k < n(如果k这一位是1),y&x == 0 所以枚举 a[i] ,如果a[i]每被标记,搜索 (~a[i])的子集, 子集的子集......,边搜索边标记元素,如果出现一个y也属于a[i],那么再搜索(~y)的子集 这样就能保证一个联通图里的元素都能在一次搜索里被标记完 代码: #include<bits/stdc…
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代码: #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair…
F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience. This country has n cities connected by m bidirectional roads of different lengths…
F. Teodor is not a liar! time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segm…
F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree with n nodes (numbered from 1 to n) rooted at node 1. Also, each node has two values associated…