AtCoder Beginner Contest 292——A-E题讲解

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻讲解一下AtCoder Beginner Contest 292这场比赛的A-E题

===========================================================================================

A题

原题

Problem Statement

You are given a string S S S consisting of lowercase English letters.
Uppercase each character of S S S and print the resulting string T T T.

Constraints

S S S is a string consisting of lowercase English letters whose length is between 1 1 1 and 100 100 100, inclusive.

Input

The input is given from Standard Input in the following format:
S S S

Output

Print T T T.

Sample Input 1

abc

Sample Output 1

ABC
Uppercase each character of abc, and you have ABC.

Sample Input 2

a

Sample Output 2

A

Sample Input 3

abcdefghjiklnmoqprstvuwxyz

Sample Output 3

ABCDEFGHJIKLNMOQPRSTVUWXYZ

思路

水题一道,不多写啦!

代码

/*
------------------Welcome to Your Code--------------
Name:
Contest:AtCoder Beginner Contest 292
Wishes:AK!
------------------Start Writing!!!------------------
*/
#include <iostream>
#define endl '\n'
#define pb(i) push_back(i)

using namespace std;

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    string s;
    
    cin >> s;
    
    for (auto c : s)
    	cout << char(c - 'a' + 'A');
    
    return 0;
}

B题

原题

Problem Statement

N N N players numbered 1 1 1 to N N N will play a soccer game.

When a player commits an offense, that player will receive a yellow card or a red card.

A player who satisfies one of the following conditions will be removed from the game.
Accumulates two yellow cards.
Receives a red card.
Once a player is removed, that player will no longer receive any cards.
You will watch this game. Initially, the players have not received any cards.

There will be Q Q Q events. Correctly answer the questions asked in the events.

There are three kinds of events, which are given in the format c x from the input, where c c c is 1 1 1, 2 2 2, or 3 3 3. The events are as follows.
1 x: Player x x x receives a yellow card.
2 x: Player x x x receives a red card.
3 x: You are asked whether player x x x has been removed from the game. Answer Yes or No.

Constraints

1 ≤ N ≤ 100 1 \leq N \leq 100 1N100
1 ≤ Q ≤ 100 1 \leq Q \leq 100 1Q100
1 ≤ x ≤ N 1 \leq x \leq N 1xN in all events.
There is at least one event of the third kind.
A player who has been removed will no longer receive any cards.
All values in the input are integers.

Input

The input is given from Standard Input in the following format, where event i \text{event}_i eventi denotes the i i i-th event.
N N N Q Q Q
event 1 \text{event}_1 event1
event 2 \text{event}_2 event2
⋮ \vdots
event Q \text{event}_Q eventQ
Each event is in one of the following formats:
1 x x x
2 x x x
3 x x x

Output

Print X X X lines, where X X X is the number of events of the third kind in the input.

The i i i-th line should contain Yes if, for the i i i-th event of the third kind, player x x x has been removed from the game, and No otherwise.

Sample Input 1

3 9
3 1
3 2
1 2
2 1
3 1
3 2
1 2
3 2
3 3

Sample Output 1

No
No
Yes
No
Yes
No
Here are all the events in chronological order.
In the 1 1 1-st event, you are asked whether player 1 1 1 has been removed from the game. Player 1 1 1 has not been removed, so you should print No.

In the 2 2 2-nd event, you are asked whether player 2 2 2 has been removed from the game. Player 2 2 2 has not been removed, so you should print No.

In the 3 3 3-rd event, player 2 2 2 receives a yellow card.

In the 4 4 4-th event, player 1 1 1 receives a red card and is removed from the game.

In the 5 5 5-th event, you are asked whether player 1 1 1 has been removed from the game. Player 1 1 1 has been removed, so you should print Yes.

In the 6 6 6-th event, you are asked whether player 2 2 2 has been removed from the game. Player 2 2 2 has not been removed, so you should print No.

In the 7 7 7-th event, player 2 2 2 receives a yellow card and is removed from the game.

In the 8 8 8-th event, you are asked whether player 2 2 2 has been removed from the game. Player 2 2 2 has been removed, so you should print Yes.

In the 9 9 9-th event, you are asked whether player 3 3 3 has been removed from the game. Player 3 3 3 has not been removed, so you should print No.

思路

也比较水,不讲啦,直接看代码吧!

代码

/*
------------------Welcome to Your Code--------------
Name:
Contest:AtCoder Beginner Contest 292
Wishes:AK!
------------------Start Writing!!!------------------
*/
#include <iostream>
#define endl '\n'
#define pb(i) push_back(i)

using namespace std;

const int N = 1e2 + 10;

double st[N];

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    int n, q;
    
    cin >> n >> q;
    
    while (q --)
    {
    	int op, x;
    	cin >> op >> x;
    	
    	if (op == 1) st[x] += 0.5;
    	else if (op == 2) st[x] ++;
    	else
    	{
    		cout << (st[x] >= 1 ? "Yes" : "No") << endl;
    	}
    }
    
    return 0;
}

C题

原题

Problem Statement

You are given a positive integer N N N.

Find the number of quadruples of positive integers ( A , B , C , D ) (A,B,C,D) (A,B,C,D) such that A B + C D = N AB + CD = N AB+CD=N.
Under the constraints of this problem, it can be proved that the answer is at most 9 × 1 0 18 9 \times 10^{18} 9×1018.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2N2×105
N N N is an integer.

Input

The input is given from Standard Input in the following format:
N N N

Output

Print the answer.

Sample Input 1

4

Sample Output 1

8
Here are the eight desired quadruples.
( A , B , C , D ) = ( 1 , 1 , 1 , 3 ) (A,B,C,D)=(1,1,1,3) (A,B,C,D)=(1,1,1,3)
( A , B , C , D ) = ( 1 , 1 , 3 , 1 ) (A,B,C,D)=(1,1,3,1) (A,B,C,D)=(1,1,3,1)
( A , B , C , D ) = ( 1 , 2 , 1 , 2 ) (A,B,C,D)=(1,2,1,2) (A,B,C,D)=(1,2,1,2)
( A , B , C , D ) = ( 1 , 2 , 2 , 1 ) (A,B,C,D)=(1,2,2,1) (A,B,C,D)=(1,2,2,1)
( A , B , C , D ) = ( 1 , 3 , 1 , 1 ) (A,B,C,D)=(1,3,1,1) (A,B,C,D)=(1,3,1,1)
( A , B , C , D ) = ( 2 , 1 , 1 , 2 ) (A,B,C,D)=(2,1,1,2) (A,B,C,D)=(2,1,1,2)
( A , B , C , D ) = ( 2 , 1 , 2 , 1 ) (A,B,C,D)=(2,1,2,1) (A,B,C,D)=(2,1,2,1)
( A , B , C , D ) = ( 3 , 1 , 1 , 1 ) (A,B,C,D)=(3,1,1,1) (A,B,C,D)=(3,1,1,1)

Sample Input 2

292

Sample Output 2

10886

Sample Input 3

19876

Sample Output 3

2219958


思路

这道题我们可以枚举 A B AB AB,假设为 i i i,那么 C D CD CD就是 n − i n - i ni,之后先计算约数的个数,如果是个奇数,那么就说明中间有个数是相同的,所以要特判。最后答案就把各种情况列出来相加(令nab为AB的约数的个数,ncd为CD的约数的个数):

  • 若nab,ncd都为偶数,则:此时的方案数 = n a b × n c d =nab\times ncd =nab×ncd
  • 若nab,ncd都为奇数,则:此时的方案数 = ( n a b − 1 ) ( n c d − 1 ) + ( n a b − 1 ) + ( n c d − 1 ) + 1 =(nab-1)(ncd-1)+(nab - 1)+(ncd - 1)+1 =(nab1)(ncd1)+(nab1)+(ncd1)+1,可能有点难理解,自己推一下就可以啦!(若还是不懂,联系我即可,评论区做了解释了!)
  • 若nab为奇数,ncd为偶数,则:此时的方案数 = ( n a b − 1 ) n c d + n c d × 2 =(nab - 1)ncd+ncd\times 2 =(nab1)ncd+ncd×2

最后,把所有的方案数相加,就是最终的答案了!


代码(时间复杂度: O ( N N ) O(N\sqrt{N}) O(NN )

/*
------------------Welcome to Your Code--------------
Name:
Contest:AtCoder Beginner Contest 292
Wishes:AK!
------------------Start Writing!!!------------------
*/
#include <iostream>
#include <cmath>
#define endl '\n'
#define pb(i) push_back(i)

using namespace std;

int res;

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}


int get_divides(int n) //求约数的个数
{
    int counts = 0;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i * i == n) 
                counts++;
            else
                counts += 2;
        }
    }
    return counts;
}

int main()
{
    int n;
    cin >> n;
    long long ans = 0;
    for (int i = 1; i < n; i ++)
    {
    	int ab = i, cd = n - i;
    	
    	int na,nab = get_divides(ab);
    	bool fab= 0;
    	
    	int nc,ncd = get_divides(cd);
    	bool fcd= 0;
    	
    	if(nab % 2) na = nab -1,fab =1;
    	else na = nab;
    	
    	if(ncd % 2) nc = ncd -1,fcd =1;
    	else nc = ncd;
    	
    	if(fab ){
    		if(fcd){
    			ans += na * nc  + na + nc + 1;
    			
    		}
    		else{
    			ans += na * nc + nc * 2 ;
    		}
    	}
    	else ans += na * nc;

    }
    
    cout << ans << endl;
    
    return 0;
}

D题

原题

Problem Statement

You are given an undirected graph with N N N vertices numbered 1 1 1 to N N N and M M M edges numbered 1 1 1 to M M M. Edge i i i connects vertex u i u_i ui and vertex v i v_i vi.
Determine whether every connected component in this graph satisfies the following condition.
The connected component has the same number of vertices and edges.

Notes

An undirected graph is a graph with edges without direction.

A subgraph of a graph is a graph formed from a subset of vertices and edges of that graph.

A graph is connected when one can travel between every pair of vertices in the graph via edges.

A connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
0 ≤ M ≤ 2 × 1 0 5 0 \leq M \leq 2 \times 10^5 0M2×105
1 ≤ u i ≤ v i ≤ N 1 \leq u_i \leq v_i \leq N 1uiviN
All values in the input are integers.

Input

The input is given from Standard Input in the following format:
N N N M M M
u 1 u_1 u1 v 1 v_1 v1
⋮ \vdots
u M u_M uM v M v_M vM

Output

If every connected component satisfies the condition, print Yes; otherwise, print No.

Sample Input 1

3 3
2 3
1 1
2 3

Sample Output 1

Yes
The graph has a connected component formed from just vertex 1 1 1, and another formed from vertices 2 2 2 and 3 3 3.

The former has one edge (edge 2 2 2), and the latter has two edges (edges 1 1 1 and 3 3 3), satisfying the condition.

Sample Input 2

5 5
1 2
2 3
3 4
3 5
1 5

Sample Output 2

Yes

Sample Input 3

13 16
7 9
7 11
3 8
1 13
11 11
6 11
8 13
2 11
3 3
8 12
9 11
1 11
5 13
3 12
6 9
1 10

Sample Output 3

No


思路

这道题我们就是判断每一个连通块是否点数和边数相等,所以我们可以用**洪水填充(Flood Fill)**算法,当然可以用DFS做!


代码

/*
------------------Welcome to Your Code--------------
Name:
Contest:AtCoder Beginner Contest 292
Wishes:AK!
------------------Start Writing!!!------------------
*/
#include <iostream>
#include <vector>

using namespace std;

const int N = 2e5 + 10;

int n, m, edge, vert;
vector<int> fg[N];
bool ft[N];

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}

void dfs(int u)
{
	for (auto c : fg[u]){
		
	
		if (!ft[c])
		{
			ft[c] = 1;
			edge ++, vert ++;
			dfs(c);
		}
		else edge ++;
	}
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> n >> m;
    
    int x, y;
    for (int i = 1; i <= m; i ++)
    	cin >> x >> y, fg[x].push_back(y), fg[y].push_back(x);
    	
    for (int i = 1; i <= n; i ++)
    	if (!ft[i])
    	{
    		vert = 1;
    		edge = 0;
    		ft[i] = 1;
    		dfs(i); //找当前连通块
    
    		if (edge / 2 != vert) //因为我是用的无向边,所以真正的边数要除以2
    		{
    			cout << "No" << endl;
    			return 0;
    		}

    	}
    	
    cout << "Yes" << endl;
    return 0;
}

E题

原题

Problem Statement

You are given a simple directed graph with N N N vertices numbered 1 1 1 to N N N and M M M edges numbered 1 1 1 to M M M. Edge i i i is a directed edge from vertex u i u_i ui to vertex v i v_i vi.
You may perform the following operation zero or more times.
Choose distinct vertices x x x and y y y such that there is no directed edge from vertex x x x to vertex y y y, and add a directed edge from vertex x x x to vertex y y y.
Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.
For every triple of distinct vertices a a a, b b b, and c c c, if there are directed edges from vertex a a a to vertex b b b and from vertex b b b to vertex c c c, there is also a directed edge from vertex a a a to vertex c c c.

Constraints

3 ≤ N ≤ 2000 3 \leq N \leq 2000 3N2000
0 ≤ M ≤ 2000 0 \leq M \leq 2000 0M2000
1 ≤ u i , v i ≤ N 1 \leq u_i ,v_i \leq N 1ui,viN
u i ≠ v i u_i \neq v_i ui=vi
( u i , v i ) ≠ ( u j , v j ) (u_i,v_i) \neq (u_j,v_j) (ui,vi)=(uj,vj) if i ≠ j i \neq j i=j.
All values in the input are integers.

Input

The input is given from Standard Input in the following format:
N N N M M M
u 1 u_1 u1 v 1 v_1 v1
⋮ \vdots
u M u_M uM v M v_M vM

Output

Print the answer.

Sample Input 1

4 3
2 4
3 1
4 3

Sample Output 1

3
Initially, the condition is not satisfied because, for instance, for vertices 2 2 2, 4 4 4, and 3 3 3, there are directed edges from vertex 2 2 2 to vertex 4 4 4 and from vertex 4 4 4 to vertex 3 3 3, but not from vertex 2 2 2 to vertex 3 3 3.
You can make the graph satisfy the condition by adding the following three directed edges:
one from vertex 2 2 2 to vertex 3 3 3,
one from vertex 2 2 2 to vertex 1 1 1, and
one from vertex 4 4 4 to vertex 1 1 1.
On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 3 3 3.

Sample Input 2

292 0

Sample Output 2

0

Sample Input 3

5 8
1 2
2 1
1 3
3 1
1 4
4 1
1 5
5 1

Sample Output 3

12


思路

这道题我们完全可以把每个点能到的点的个数都加起来,在减去原来就有的边数,就是我们没有建出来的边数,所以求出这个没有建的边数即可!


代码

/*
------------------Welcome to Your Code--------------
Name:
Contest:AtCoder Beginner Contest 292
Wishes:AK!
------------------Start Writing!!!------------------
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#define endl '\n'
#define pb(i) push_back(i)

using namespace std;

const int N = 2e3 + 10;

int n, m;
bool edge[N][N];
vector<int> g[N];
int x, y;
int turn[N];
bool st[N];

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}

int bfs(int u)
{
	memset(st, 0, sizeof st);
	
	queue<int> q;
	q.push(u);
	st[u] = 1;
	
	int res = 0;
	
	while (q.size())
	{
		int t = q.front();
		q.pop();
		
		for (auto c : g[t])
			if (!st[c])
			{
				q.push(c);
				res ++;
				st[c] = 1;
			}
	}
	
	return res;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> n >> m;
    
    for (int i = 1; i <= m; i ++)
    	cin >> x >> y, g[x].pb(y), edge[x][y] = 1;
    	
    int ans = 0;
    for (int i = 1; i <= n; i ++)
    	ans += bfs(i);
    	
    cout << ans - m << endl;
    
    return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值