codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )
2 seconds
256 megabytes
standard input
standard output
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 is not listed in the input, then there is an edge between these vertices.
You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.
The first line contains two integers n and m (1 ≤ n ≤ 200000, ).
Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there existsan edge between those vertices.
Firstly print k — the number of connected components in this graph.
Then print k integers — the sizes of components. You should output these integers in non-descending order.
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4
题意:一个无向图,给出没有连边的点对(没有给出的点对都有直接连边),求联通块个数和每个联通块的大小。
题解:首先是存边的问题,不可能把所有边都记录下来吧。那么久只能记录不连通关系了,用邻接表记录的话,询问两个点之间是否有连边不大方便。
可以给每个点开一个set,set里面存不连通关系,询问两个点之间是否有连边就在set里面查找有没有对应元素就行了。
也可以给每个点开一个map,当做邻接矩阵用。我采用的是这种。
思路:维护一个集合(set),存储当前不确定在哪个联通块中的点,初始时所有点都在里面。
然后在其中依次取点,dfs遍历它的联通块,统计一下,把遍历到的点都扔出这个集合,因为它们的连通关系已经确定,不用再对其进行dfs。
注意:dfs一个点x的时候,要寻找x的出边,这样很慢,应该在set中依次查找,判断set中的点与x是否有直接连边。
遍历set的时候不要for(set<int>iterator::it=s.begin();it!=s.end();it++) ,由于有删除操作,还是递归删除,所以it指向的元素可能在下一层递归中被删掉,然后……就RE了。可以用lower_bound()或upper_bound()查找下一个元素,具体见代码。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
const int maxn=;
int N,M,t1,t2,temp[maxn],ans;
set<int>s;
map<int,bool>e[maxn];
void dfs(int x){
int prev=;
while(){
int t=*s.lower_bound(prev);
if(t==(<<))
break;
prev=t+;
if(!e[x][t]){
temp[ans]++;
s.erase(t);
dfs(t);
}
}
}
int main(){
//freopen("in","r",stdin);
N=read(),M=read();
for(int i=;i<=N;i++)
s.insert(i);
s.insert(<<);
for(int i=;i<=M;i++){
t1=read(),t2=read();
e[t1][t2]=e[t2][t1]=;
}
int prev=;
while(){
int t=*s.lower_bound(prev);
if(t==(<<))
break;
prev=t+;
s.erase(t);
temp[++ans]=;
dfs(t);
}
sort(temp+,temp++ans);
printf("%d\n",ans);
for(int i=;i<=ans;i++)
printf("%d ",temp[i]);
puts("");
return ;
}
2 seconds
256 megabytes
standard input
standard output
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
- REPLACE l r — for every replace ai with D(ai);
- SUM l r — calculate .
Print the answer for each SUM query.
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
For each SUM query print the answer to it.
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
30
13
4
22
大意:对于一个给定序列,有两种操作:
1:给定区间[L,R],将其中的每个元素x变为D(x)
D(x)是x的因子数。
2:给定区间[L,R],求和。
题解:
线段树套路题。
注意到在x属于1e6以内时,D(x)最大是240.
x,D(x),D(D(x))……的衰减速度非常快。
打表可知,对于一个数1e6以内的x,最多进行第一个操作6次,就会变成2(除了1 , D(1)=1)
先把序列中的所有1都换成2,同时记录一下,方便统计回来。
维护一个线段树记录区间内1操作数的次数的最小值和区间和
对于每个1操作,暴力修改到线段树底层,直到1操作次数最小值大于等于6.
这样修改的复杂度最差O(N*logN) (最多修改6次,6是常数)
查询的复杂度为O(1)
UOJ round 和 hdu 上都有类似的题。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
inline int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
inline int mymin(int xx,int yy)
{if(xx<yy)return xx;return yy;}
const int maxn=,limit=;
int N,M,D[limit+],tot=,sum[maxn],a[maxn];
struct Segment_Tree{
long long sum;
}T[maxn*];
int x,y,opt;
void build(int L,int R,int root){
if(L==R){
T[root].sum=a[L];
return;
}
int mid=(L+R)>>;
build(L,mid,root*);
build(mid+,R,root*+);
T[root].sum=T[root*].sum+T[root*+].sum;
}
void upd(int L,int R,int root){
if(T[root].sum==(R-L+)*)
return;
if(x>R||y<L)
return;
if(L==R){
T[root].sum=D[T[root].sum];
return;
}
int mid=(L+R)>>;
upd(L,mid,root*);
upd(mid+,R,root*+);
T[root].sum=T[root*].sum+T[root*+].sum;
}
long long query(int L,int R,int root){
if(x>R||y<L)
return ;
if(x<=L&&y>=R)
return T[root].sum;
int mid=(L+R)>>;
return query(L,mid,root*)+query(mid+,R,root*+);
}
int main(){
//freopen("in","r",stdin);
for(int i=;i<=limit;i++)
for(int j=i;j<=limit;j+=i)
D[j]++;
N=read(),M=read();
for(int i=;i<=N;i++){
a[i]=read();
sum[i]=sum[i-];
if(a[i]==)
a[i]=,sum[i]++;
}
build(,N,);
while(M--){
opt=read();
if(opt==){
x=read(),y=read();
upd(,N,);
}
else{
x=read(),y=read();
printf("%I64d\n",query(,N,)-(sum[y]-sum[x-]));
}
}
return ;
}
5 seconds
256 megabytes
standard input
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 common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p)are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.
You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).
The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.
Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 106).
Print t integers, where i-th integer is the answer to i-th query.
3
7 22 1
7 22 2
7 22 3
9
13
15
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
187
87
139
128
141
大意:t个询问,给出x,p,k求与p互质的大于x的第k个数。
5 seconds
256 megabytes
standard input
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 common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p)are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.
You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).
The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.
Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 106).
Print t integers, where i-th integer is the answer to i-th query.
3
7 22 1
7 22 2
7 22 3
9
13
15
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
187
87
139
128
141
题解:二分加容斥。
每个数的因子可以用筛法筛出来。
然后就是二分查找ans,容斥原理计算ans内有多少个与p互质的数。
至于大于x嘛,只需用容斥算出小于等于x的与p互质的数有多少个,加进k里面就行了。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
const int limit=;
vector<int>V[limit+];
void get_fac(){
for(int i=;i<=limit;i++)
if(!V[i].size())
for(int j=i;j<=limit;j+=i)
V[j].push_back(i);
}
int x,p,k,delta;
int L,R,mid;
int calc(int num,int rb){
int siz=V[num].size();
int re=;
for(int i=;i<(<<siz);i++){
int s=,tim=;
for(int j=;j<siz;j++)
if((i>>j)&)
s*=V[num][j],tim++;
if(tim&)
re+=rb/s;
else
re-=rb/s;
}
return rb-re;
}
int main(){
//freopen("in","r",stdin);
get_fac();
for(int T=read();T;T--){
x=read(),p=read(),k=read();
delta=calc(p,x);
L=x,R=int(1e8);
while(L+<R){
mid=(L+R)/;
if(calc(p,mid)-delta>=k)
R=mid;
else
L=mid;
}
printf("%d\n",R);
}
return ;
}
codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )的更多相关文章
- 【codeforces】【比赛题解】#920 Educational CF Round 37
[A]浇花 题意: 一个线段上每个整点都有花,有的点有自动浇花的喷水器,有问几秒能浇完所有的花. 题解: 大模拟 #include<cstdio> #include<cstring& ...
- 【codeforces】【比赛题解】#915 Educational CF Round 36
虽然最近打了很多场CF,也涨了很多分,但是好久没写CF的题解了. 前几次刚刚紫名的CF,太伤感情了,一下子就掉下来了,不懂你们Div.1. 珂学的那场我只做了第一题……悲伤. 这次的Education ...
- Codeforces - tag::flows 大合集 [完坑 x14]
589F 题意:给出n个时间区间,每个区间挑定长的非连续区间,求不同个区间不存在时间冲突的最大定长,输出乘上n 二分图模型+二分长度,左顶点集为区间编号,右顶点集为时间编号(1...10000),汇点 ...
- Codeforces - tag::dp 大合集 [占坑 6 / inf]
Gym - 100753J 某国家仅有金币和银币两种货币,起汇率为g,纪念品市场有n个商人和商品,商人结帐只用银币,并且把一堆银币装在袋子里,分为三种类型,分别按向下/向上/四舍五入取整(其中向上的优 ...
- Codeforces - tag::graphs 大合集 [占坑]
520B 给定初始n和目标m,存在两种操作\(-1\)和\(×2\),要求最少操作次数 无脑解法是BFS,不能解决稍大的规模 当n>m时,输出n-m 否则逆向处理,转换为m到n的最小操作次数,存 ...
- 题解合集 (update on 11.5)
收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...
- 【题解】AC自动机题解合集
最近貌似大家都在搞字符串?很长一段时间都没有写博客了……还是补一补坑吧. 感觉AC自动机真的非常优美了,通过在trie树上建立fail指针可以轻松解决多模匹配的问题.实际上在AC自动机上的匹配可以看做 ...
- 【CSP2019】题解合集
诈个尸 先挖坑 虽然连去都没去但还是想做做 今年貌似比去年还毒瘤啊... yrx.hjw都进了省队线tql orz (myh:没AK真丢脸 Day1T1 格雷码 Day1T2 括号树 Day1T3 树 ...
- CF codeforces A. New Year Garland【Educational Codeforces Round 79 (Rated for Div. 2)】
A. New Year Garland time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- java动态代理实现与原理详细分析(【转载】By--- Gonjan )
[转载]By---Gonjan 关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式 ...
- 浅谈IFC
IFC布局规则: 在一个行内格式化上下文中,盒是一个接一个水平放置的,从包含块的顶部开始 这些盒之间的水平margin,border和padding都有效 盒可能以不同的方式竖直对齐:以它们的底部或者 ...
- block:cfq 学习02
From: https://blog.csdn.net/vanbreaker/article/details/8308766 前文介绍了CFQ调度器的一些概念和结构之间的关系,这里再结合实际的代码,来 ...
- Docker 安装并定制 Nginx 服务器
安装并定制 Nginx 1.查阅对应的官方文档,首先下载镜像文件: [spider@izwz9d74k4cznxtxjeeur9z local]$ sudo docker pull nginx [su ...
- Script:shell脚本生成随机字符串
#!/bin/bash # bash generate random alphanumeric string # # bash generate random character alphanumer ...
- 洛谷——P1972 [SDOI2009]HH的项链(线段树)
P1972 [SDOI2009]HH的项链 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的 ...
- Tampermonkey脚本安装问题及自用脚本推荐
对于高手来说,chrome浏览器中即使没有其他任何chrome插件,可能都无关紧要.但是有一个插件必不可少, 那就是Tampermonkey油猴插件.Tampermonkey是Chrome上最流行的用 ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- 洛谷 2777 [AHOI2016初中组]自行车比赛
[题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...
- BZOJ 1012 洛谷1198 最大数 maxnumber
用线段数维护即可 #include<cstdio> #include<algorithm> #define ls (cur<<1) #define rs (cur& ...