codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

允我心安 提交于 2021-02-16 19:59:37
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()查找下一个元素,具体见代码。

 

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 #include<map>
14 #include<set>
15 using namespace std;
16 int read(){
17     int xx=0,ff=1;char ch=getchar();
18     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
19     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
20     return xx*ff;
21 }
22 const int maxn=200010;
23 int N,M,t1,t2,temp[maxn],ans;
24 set<int>s;
25 map<int,bool>e[maxn];
26 void dfs(int x){
27     int prev=0;
28     while(1){
29         int t=*s.lower_bound(prev);
30         if(t==(1<<30))
31             break;
32         prev=t+1;
33         if(!e[x][t]){
34             temp[ans]++;
35             s.erase(t);
36             dfs(t);
37         }
38     }
39 }
40 int main(){
41     //freopen("in","r",stdin);
42     N=read(),M=read();
43     for(int i=1;i<=N;i++)
44         s.insert(i);
45     s.insert(1<<30);
46     for(int i=1;i<=M;i++){
47         t1=read(),t2=read();
48         e[t1][t2]=e[t2][t1]=1;
49     }
50     int prev=0;
51     while(1){
52         int t=*s.lower_bound(prev);
53         if(t==(1<<30))
54             break;
55         prev=t+1;
56         s.erase(t);
57         temp[++ans]=1;
58         dfs(t);
59     }
60     sort(temp+1,temp+1+ans);
61     printf("%d\n",ans);
62     for(int i=1;i<=ans;i++)
63         printf("%d ",temp[i]);
64     puts("");
65     return 0;
66 }
View Code

 

 

 

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 上都有类似的题。

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 using namespace std;
14 inline int read(){
15     int xx=0,ff=1;char ch=getchar();
16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
18     return xx*ff;
19 }
20 inline int mymin(int xx,int yy)
21 {if(xx<yy)return xx;return yy;}
22 const int maxn=300010,limit=1000000;
23 int N,M,D[limit+10],tot=6,sum[maxn],a[maxn];
24 struct Segment_Tree{
25     long long sum;
26 }T[maxn*4];
27 int x,y,opt;
28 void build(int L,int R,int root){
29     if(L==R){
30         T[root].sum=a[L];
31         return;
32     }
33     int mid=(L+R)>>1;
34     build(L,mid,root*2);
35     build(mid+1,R,root*2+1);
36     T[root].sum=T[root*2].sum+T[root*2+1].sum;
37 }
38 void upd(int L,int R,int root){
39     if(T[root].sum==(R-L+1)*2)
40         return;
41     if(x>R||y<L)
42         return;
43     if(L==R){
44         T[root].sum=D[T[root].sum];
45         return;
46     }
47     int mid=(L+R)>>1;
48     upd(L,mid,root*2);
49     upd(mid+1,R,root*2+1);
50     T[root].sum=T[root*2].sum+T[root*2+1].sum;
51 }
52 long long query(int L,int R,int root){
53     if(x>R||y<L)
54         return 0;
55     if(x<=L&&y>=R)
56         return T[root].sum;
57     int mid=(L+R)>>1;
58     return query(L,mid,root*2)+query(mid+1,R,root*2+1);
59 }
60 int main(){
61     //freopen("in","r",stdin);
62     for(int i=1;i<=limit;i++)
63         for(int j=i;j<=limit;j+=i)
64             D[j]++;
65     N=read(),M=read();
66     for(int i=1;i<=N;i++){
67         a[i]=read();
68         sum[i]=sum[i-1];
69         if(a[i]==1)
70             a[i]=2,sum[i]++;
71     }
72     build(1,N,1);
73     while(M--){
74         opt=read();
75         if(opt==1){
76             x=read(),y=read();
77             upd(1,N,1);
78         }
79         else{
80             x=read(),y=read();
81             printf("%I64d\n",query(1,N,1)-(sum[y]-sum[x-1]));
82         }
83     }
84     return 0;
85 }
View Code

 

 

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里面就行了。

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 #include<vector>
14 using namespace std;
15 int read(){
16     int xx=0,ff=1;char ch=getchar();
17     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
18     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
19     return xx*ff;
20 }
21 const int limit=1000000;
22 vector<int>V[limit+5];
23 void get_fac(){
24     for(int i=2;i<=limit;i++)
25         if(!V[i].size())
26             for(int j=i;j<=limit;j+=i)
27                 V[j].push_back(i);
28 }
29 int x,p,k,delta;
30 int L,R,mid;
31 int calc(int num,int rb){
32     int siz=V[num].size();
33     int re=0;
34     for(int i=1;i<(1<<siz);i++){
35         int s=1,tim=0;
36         for(int j=0;j<siz;j++)
37             if((i>>j)&1)
38                 s*=V[num][j],tim++;
39         if(tim&1)
40             re+=rb/s;
41         else
42             re-=rb/s;
43     }
44     return rb-re;
45 }
46 int main(){
47     //freopen("in","r",stdin);
48     get_fac();
49     for(int T=read();T;T--){
50         x=read(),p=read(),k=read();
51         delta=calc(p,x);
52         L=x,R=int(1e8);
53         while(L+1<R){
54             mid=(L+R)/2;
55             if(calc(p,mid)-delta>=k)
56                 R=mid;
57             else
58                 L=mid;
59         }
60         printf("%d\n",R);
61     }
62     return 0;
63 }
View Code

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!