E. Connected Components?
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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 ≤ nx ≠ 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.

Output

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.

Example
input
5 5
1 2
3 4
3 2
4 2
2 5
output
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 ;
}
F. SUM and REPLACE
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. REPLACE l r — for every  replace ai with D(ai);
  2. SUM l r — calculate .

Print the answer for each SUM query.

Input

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 tiliri 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.

Output

For each SUM query print the answer to it.

Example
input
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
output
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 ;
}
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 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 xp and k, and the answer to this query is k-th element of L(x, p).

Input

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 xp and k for i-th query (1 ≤ x, p, k ≤ 106).

Output

Print t integers, where i-th integer is the answer to i-th query.

Examples
input
3
7 22 1
7 22 2
7 22 3
output
9
13
15
input
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
output
187
87
139
128
141

大意:t个询问,给出x,p,k求与p互质的大于x的第k个数。

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 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 xp and k, and the answer to this query is k-th element of L(x, p).

Input

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 xp and k for i-th query (1 ≤ x, p, k ≤ 106).

Output

Print t integers, where i-th integer is the answer to i-th query.

Examples
input
3
7 22 1
7 22 2
7 22 3
output
9
13
15
input
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
output
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 )的更多相关文章

  1. 【codeforces】【比赛题解】#920 Educational CF Round 37

    [A]浇花 题意: 一个线段上每个整点都有花,有的点有自动浇花的喷水器,有问几秒能浇完所有的花. 题解: 大模拟 #include<cstdio> #include<cstring& ...

  2. 【codeforces】【比赛题解】#915 Educational CF Round 36

    虽然最近打了很多场CF,也涨了很多分,但是好久没写CF的题解了. 前几次刚刚紫名的CF,太伤感情了,一下子就掉下来了,不懂你们Div.1. 珂学的那场我只做了第一题……悲伤. 这次的Education ...

  3. Codeforces - tag::flows 大合集 [完坑 x14]

    589F 题意:给出n个时间区间,每个区间挑定长的非连续区间,求不同个区间不存在时间冲突的最大定长,输出乘上n 二分图模型+二分长度,左顶点集为区间编号,右顶点集为时间编号(1...10000),汇点 ...

  4. Codeforces - tag::dp 大合集 [占坑 6 / inf]

    Gym - 100753J 某国家仅有金币和银币两种货币,起汇率为g,纪念品市场有n个商人和商品,商人结帐只用银币,并且把一堆银币装在袋子里,分为三种类型,分别按向下/向上/四舍五入取整(其中向上的优 ...

  5. Codeforces - tag::graphs 大合集 [占坑]

    520B 给定初始n和目标m,存在两种操作\(-1\)和\(×2\),要求最少操作次数 无脑解法是BFS,不能解决稍大的规模 当n>m时,输出n-m 否则逆向处理,转换为m到n的最小操作次数,存 ...

  6. 题解合集 (update on 11.5)

    收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...

  7. 【题解】AC自动机题解合集

    最近貌似大家都在搞字符串?很长一段时间都没有写博客了……还是补一补坑吧. 感觉AC自动机真的非常优美了,通过在trie树上建立fail指针可以轻松解决多模匹配的问题.实际上在AC自动机上的匹配可以看做 ...

  8. 【CSP2019】题解合集

    诈个尸 先挖坑 虽然连去都没去但还是想做做 今年貌似比去年还毒瘤啊... yrx.hjw都进了省队线tql orz (myh:没AK真丢脸 Day1T1 格雷码 Day1T2 括号树 Day1T3 树 ...

  9. 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 ...

随机推荐

  1. TWaver HTML5之树形布局

    转眼间春节假期已经过完,作为一个职业的程序猿,不知道大家有没有这样的感觉,一天不碰电脑,总觉得生活少点什么.今天是春节后上班的第三天,给大家分享一下我们前段时间的一个需求,需求是这样的:界面中的网元分 ...

  2. 第四章 模块化React和Redux应用

    第四章 模块化React和Redux应用 4.1 模块化应用要点 构建一个应用的基础: 代码文件的组织结构: 确定模块的边界: Store的状态树设计. 4.2 代码文件的组织方式 4.2.1 按角色 ...

  3. 第3章 从Flux到Redux

    第3章 从Flux到Redux 3.1 Flux 单向数据流,React是用来替换Jquery的,Flux是以替换Backbone.js.Ember.js等MVC框架为主的. actionTypes. ...

  4. Spring核心技术(一)——IoC容器和Bean简介

    IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...

  5. 【Codeforces 1102E】Monotonic Renumeration

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 会发现如果a[i]=a[j] 那么b[i]~b[j]都是相同的,等于b[i] 而b[i]等于b[i-1]+1或者b[i] 有两种可能 所以对于 ...

  6. 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)-----https://www.cnblogs.com/smileberry/p/4145872.html

    https://www.cnblogs.com/smileberry/p/4145872.html 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件(转)

  7. [luoguP2158] [SDOI2008]仪仗队(数论)

    传送门 可以看出 (i, j) 能被看到,(i * k, j * k) 都会被挡住 暴力 所以 gcd(i, j) == 1 的话 ans ++ 那么可以枚举一半(中轴对称),求解答案,只能拿30分 ...

  8. [luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

    传送门 有向图,找点数大于1的强连通分量个数 ——代码 #include <stack> #include <cstdio> #include <cstring> ...

  9. [USACO1.2]挤牛奶Milking Cows

    题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...

  10. Docker Command

    1. #docker inspect id           这个命令给出和容器相关的所有信息(https://www.imooc.com/video/15730) 2. #docker searc ...