CodeForces 1151E Number of Components】的更多相关文章

题目链接:http://codeforces.com/problemset/problem/1151/E 题目大意: n个人排成一个序列,标号为 1~n,第 i 个人的学习成绩为 ai,现在要选出学习成绩在区间 [l, r] 中的人,被选出的人如果他们在序列中相邻,就将他们划分到一个小组,设 f(l, r) 表示一共可以分出的组的个数.求这个和:$ \sum_{l = 1}^n \sum_{r = 1}^n f(l, r) $. 分析: 对于每一个学生,都有作为它所在小组区间左端点和右端点的时候…
Codeforces 题目传送门 & 洛谷题目传送门 首先需发现一个性质,那就是每一个连通块所对应的是一个区间.换句话说 \(\forall l<r\),若 \(l,r\) 在同一连通块中,那 \(\forall x\in(l,r)\),\(x\) 也在 \(l,r\) 所在的连通块中. 简单证明一下罢: 若 \(a_l<a_r\),那么 \(\forall x\in(l,r)\),若 \(a_x<a_r\),那么 \(x\) 与 \(r\) 连通:若 \(a_x\ge a_r\…
大意:给定n元素序列$a$, $1\le a_i \le n$, 定义函数$f(l,r)$表示范围在$[l,r]$以内的数构成的连通块个数, 求$\sum\limits_{i=1}^{n}\sum\limits_{j=i}^{n}f(i,j)$ 对于序列$a$中一个区间$[l,r]$, 假设最小值$mi$, 最大值$ma$, 它要想构成一个连通块的充要条件是$a[l-1],a[r+1]$不在$[mi,ma]$范围内, 可以得到贡献为$mi(n-ma+1)$. 但是显然不能暴力枚举所有区间, 我们…
[CF1151E]Number of Components 题面 CF 题解 联通块个数=点数-边数. 然后把边全部挂在较小的权值上. 考虑从小往大枚举左端点,等价于每次删掉一个元素,那么删去点数,加上边数,修改一下当前值就行了. 这个东西对于任意形态的树都可以做. #include<iostream> #include<cstdio> #include<vector> using namespace std; #define ll long long #define…
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl…
Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices…
E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Given the number n, find the smallest positive integer which has exactly n divisors. It is guara…
题目链接:http://codeforces.com/problemset/problem/27/E 暴力 //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #includ…
http://codeforces.com/contest/235/problem/E 远距离orz......rng_58 证明可以见这里(可能要FQ才能看到) 还是copy一下证明吧: 记 $$f(a,b,c)=\sum\limits_{i=1}^{a}\sum\limits_{j=1}^{b}\sum\limits_{k=1}^{c}d(ijk)$$ 和 $$g(a,b,c)=\sum\limits_{gcd(i,j)=gcd(j,k)=gcd(i,k)=1}\left \lfloor \…
Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int,…
题目传送门 传送门I 传送门II 题目大意 给定一个$n\times m$的网格,每个格子上要么填$1$,要么填$-1$,有$k$个位置上的数是已经填好的,其他位置都是空的.问有多少种填法使得任意一行或一列上的数的乘积为$-1$. $1 \leqslant n, m \leqslant 10^{3}$,$1 \leqslant k < \max (n, m)$. $k$的范围醒目.那么意味着至少存在一行或者一列为空. 假设空的是一行.那么剩下的行只需要满足那一行的乘积为$-1$,而空的这一行对应…
dp(a,b,c,p) = sigma ( dp(a/p^i,b/p^j,c/p^k) * ( 1+i+j+k) ) 表示用小于等于p的素数去分解的结果有多少个 E. Number Challenge time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output Let's denote d(n) as the number of divi…
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组成的无向完全图中,接下来的$M$条无向边不存在. 问你在这个图中有多少个连通分量,并且从小到大输出每个连通分量的顶点个数. 四.思路 求无向图的连通分量个数,只需要跑一边bfs就OK了.其实dfs也可以,只是太多的“函数压栈现场保护”浪费一丢丢时间而已,慢一点点,影响其实并不大. 要注意的是,无论跑…
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an. Simon loves fractions very much. Today he wrote out number  on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a frac…
题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. 这个是详细的题解: http://codeforces.com/blog/entry/13758 代码也是按这个思路来做的,可怜我的做法改了 N 次总是得不到正确的结果--泪 #include <iostream> #include <cstdio> #include <cstd…
https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以了.记得最后给第三组留至少一个. #include<bits/stdc++.h> using namespace std; #define ll long long int n; ]; int main(){ scanf("%d",&n); ;i<n;i++) s…
[题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> using namespace std; ; int a[MAXN]; long long sum[MAXN]; template <typename T> inline void read(T &x) { T f = ; x…
Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x[i] -1 ]这段区间的的数都会走到 k * x[i]上. 所以对于每个位置都先计算出他到右边最远的覆盖位置. 然后在反着求出每个位置能往左走走到的最远的位置. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freo…
题目链接:http://codeforces.com/problemset/problem/1189/B AC代码: #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int c[maxn]; bool check(int i) { ]+a[i+]) return true; else return false; } int main() { int n; cin >> n; ;i &l…
题目链接:http://codeforces.com/problemset/problem/1167/A 思路:检索前面0 ~(n −11)个字符中是否有 8 即可. AC代码: #include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int t; cin >> t; while(t--) { int n; string a; bool fl…
Codeforces 题目传送门 & 洛谷题目传送门 一道数论与数位 dp 结合的神题 %%% 首先在做这道题之前你需要知道一个定理:对于质数 \(p\) 及 \(n,k\),最大的满足 \(p^{\alpha}\mid\dbinom{n}{k}\) 的 \(\alpha\) 为 \(k\) 与 \(n-k\) 在 \(p\) 进制下相加的进位次数.证明就考虑扩展 Lucas 定理,记 \(f(x)\) 为最大的满足 \(p^{\alpha}\mid x\) 的 \(\alpha\),那么由 \…
题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<…
题目链接 首先要知道一个性质, 一个数x的因子个数等于 a1^p1 * a2^p2*....an^pn, ai是x质因子, p是质因子的个数. 然后就可以搜了 #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #incl…
枚举两点,确定一条线段,计算每条线段的中点坐标. 按线段中点坐标排个序.找出每一种坐标有几个. 假设第x种坐标有y个,那么这些线段可以组成y*(y-1)/2种平行四边形. 累加即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; +; long long x[maxn],y[m…
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <set> using namespace std; const int N = 2e5 + 5; vector<int> result; std::set<pair<int, int> > unExistedMap; int nextPoin…
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节点相同,表示这两个点在一个环中,环的个数+1+1+1 AC代码 /************************************************************************* > File Name: E.cpp > Author: WZY > QQ:…
题意:求,其中d(x) 表示 x 的约数个数. 析:其实是一个公式题,要知道一个结论 知道这个结论就好办了. 然后就可以解决这个问题了,优化就是记忆化gcd. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #includ…
题意:给出u,v,p,对u可以进行三种变化: 1.u=(u+1)%p ; 2.u = (u+p-1)%p;  3.u = 模p下的逆元.问通过几步可以使u变成v,并且给出每一步的操作. 分析:朴素的bfs或dfs会超时或炸栈,考虑用双向bfs头尾同时搜.用map存每个数的访问状态和对应的操作编号,正向搜步长为正,反向搜步长为负.反向搜的时候要注意对应加减操作是反过来的. #include<stdio.h> #include<iostream> #include<cstring…
很常见的思想:将整体求改为统计每个部分的贡献 本题中统计[l, r]时, 每个连通块有一个重要特征, 最右端的数在[l,r]中而下一个数不在(好像是句废话 那么我们分别考虑每个点对连通块的贡献, 即它是某个连通块的右端点, 这样可以保证每个连通块只会被算一次 对于序列 \(a_1a_2\cdots a_n\) 若\(a_i < a_{i+1}\) l的范围是1~\(a_i\) r的范围是\(a_i\) ~ $ a_j-1$ ans += (a[i+1] - a[i]) * a[i]; 否则 同理…
https://codeforc.es/contest/1189/problem/B 优先考虑最大的元素怎么构造.拿两个次大的围着他就很好,但是其他的怎么安排呢?就直接降序排列就可以了. a数组还开错了. #include<bits/stdc++.h> using namespace std; typedef long long ll; int n; int a[100005]; int main() { #ifdef Yinku freopen("Yinku.in", &…