AtCoder Beginner Contest 284 A - E

Topic address: AtCoder Beginner Contest 284 - AtCoder

An unknown college student, known as Caigou by Jiang Hu
original author: jacky Li
Email : [email protected]

Time of completion:2023.1.8
Last edited: 2023.1.10

9cfc4d3e01984f6b85b18e6876ba8027.jpeg

Table of contents

Topic address: AtCoder Beginner Contest 284 - AtCoder

AtCoder Beginner Contest 284 A - E

A - Sequence of Strings

train of thought

Reference Code

B - Multi Test Cases 

train of thought

Reference Code

C - Count Connected Components

train of thought

Reference Code

D - Happy New Year 2023 

train of thought

Reference Code

E - Count Simple Paths

train of thought

Reference Code


AtCoder Beginner Contest 284 A - E

A - Sequence of Strings

train of thought

Create a string array, read in sequentially, and output in reverse order.

Reference Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

string a[12]; 

void solve()
{
	int n; cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	for(int i = n; i >= 1; i --) cout << a[i] << endl;
	return;
}

signed main()
{
    IOS; cin.tie(0); cout.tie(0);
    int T = 1;
//    cin >> T;
    while( T -- ) solve();
    return 0;
}

B - Multi Test Cases 

train of thought

T group use cases, use an odd variable to count odd numbers, input one by one and output one by one

Reference Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

void solve()
{
	int n, odd = 0; cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		int a; cin >> a;
		if(a & 1) odd ++;
	}
	cout << odd << endl;
}

signed main()
{
    IOS; cin.tie(0); cout.tie(0);
    int T = 1;
    cin >> T;
    while( T -- ) solve();
    return 0;
}

C - Count Connected Components

train of thought

Input for this question:

5 3
1 2
1 3
4 5
output:

2

The question is to find the number of connected blocks. The above example is shown in the figure below.

ab0e15e8723a47e095b7baea0a524c6d.png

 Therefore, the board that uses the union check can be directly solved

Reference Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 2e5+10, Z = 1e5+7;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

int p[K], st[K], ans;

void init()
{
	for(int i = 1; i <= K - 5; i ++) p[i] = i;
}

int find(int x)
{
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}

void solve()
{
	init();
	int n, m; cin >> n >> m;
	for(int i = 1; i <= m; i ++)
	{
		int a, b; cin >> a >> b;
		p[find(b)] = find(a);
	}
	
	for(int i = 1; i <= n; i ++)
		st[find(i)] = true;
	
	for(int i = 1; i <= n; i ++)
		if(st[i] == true) ans ++;
	cout << ans << endl;
}

signed main()
{
    IOS; cin.tie(0); cout.tie(0);
    int T = 1;
//    cin >> T;
    while( T -- ) solve();
    return 0;
}

D - Happy New Year 2023 

train of thought

This question asks to find q^2 * p = n, and the range of n reaches the size of 9e18,

It can be obtained from mathematical knowledge that p and q must have a cubic root whose value range is less than 9e18, that is, 3e6,

Moreover, it is stated in the title that there is a unique solution, and there must be a solution to make the title much simpler. You only need to judge p and q at the same time, and you can

Reference Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

int primes[M], cnt;
bool st[M];

void get_primes(int n)
{
    for (int i = 2; i <= n; i ++ )
    {
        if (st[i]) continue;
        primes[cnt ++ ] = i;
        for (int j = i + i; j <= n; j += i)
            st[j] = true;
    }
}

void solve()
{
	// 题目中说给的用例一定有解,且唯一解,使题目难度降低,因为读错题目写了两种 
	int n; cin >> n;
	for(int i = 0; i < cnt; i ++)
	{
		int t = primes[i];
		if(n % t == 0) 
		{
			n = n / t;
			if(n % t == 0)
			{
				int a = t, b = n / t;
				cout << a << ' '  << b << endl;
				break;
			}
			else 
			{
				int a = sqrt(n), b = t; // 题目中给说了有唯一确定的值,前面没看见唯一确定的值。 
				cout << a << ' ' << b << endl;
				break;
			}
		}
	}
//	int t = pow(n, 0.333333333333333333);
//	int s = lower_bound(primes + 1, primes + cnt + 1, t) - (primes + 1);
//	for(int i = s; i <= cnt; i ++)
//	{
//		if(n % (primes[i] * primes[i]) == 0)
//			{cout << primes[i] << ' ' << (n / (primes[i] * primes[i])) << endl; break;}
//	}
	return;
}

signed main()
{
    IOS; cin.tie(0); cout.tie(0);
    get_primes(3e6);
    int T = 1;
    cin >> T;
    while( T -- ) solve();
    return 0;
}

E - Count Simple Paths

train of thought

The dfs search jumps out when it exceeds 1e6, because it is recursive and cannot use return to end. To use exit(0);

Reference code 1 (vector storage)

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
#define cmp [&](PII a, PII b){ return a.y < b.y; }

const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;

using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI; 
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;

vector<vector<int>> adj(K);
vector<int> vis(K);
int ans;
int df = 0;

void dfs(int df, int x)
{
    if(vis[x]) return;
    ans ++;
    if(ans == 1000000)
    {
        cout << ans << endl;
        exit(0);
    }
    vis[x] = 1;
    for(auto y : adj[x])
    {
        dfs(df, y);
    }
    vis[x] = 0;
}

void solve()
{
    int n, m; cin >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

//  auto dfs = [&](auto dfs, int x)
//  {
//      if(vis[x]) return;
//      ans ++;
//      if(ans == 1000000)
//      {
//          cout << ans << endl;
//          exit(0);
//      }
//      vis[x] = 1;
//      for(auto y : adj[x])
//      {
//          dfs(dfs, y);
//      }
//      vis[x] = 0;
//  };
    dfs(df, 1);
    cout << ans << endl;
}

signed main()
{
    IOS; int T = 1;
    cin.tie(nullptr); 
    cout.tie(nullptr);
//    cin >> T;
    while( T -- ) solve();
    return 0;
}

Reference code 2 (adjacency list)

#include<iostream>
#include<cstring>
#define int long long
using namespace std;
const int N =1e6+10;
int ans = 1;
bool st[N];
int h[N], e[N], ne[N], idx;
void add(int a,int b)
{
    e[idx] = b , ne[idx] = h[a] , h[a] = idx++;
}

void dfs(int u)
{
    st[u] = true;
    for(int i = h[u]; i != -1; i = ne[i])
    {
        int j = e[i];
        if(st[j]) continue;
        else ans ++;
        if(ans>=1e6) 
        {
            cout<<ans<<endl;
            exit(0);
        }
        dfs(j);
    }
    st[u] = false;
}

signed main()
{
    memset(h, -1, sizeof h);
    int n, m;  cin >> n >> m;
    while(m --)
    {
        int a, b;
        cin >> a >> b;
        add(a, b); add(b, a);
    }
    dfs(1);
    cout << ans << endl;
    
}

The author has something to say

If you feel that what the blogger said is useful to you, please click to support it, and will continue to update such issues...

Guess you like

Origin blog.csdn.net/weixin_62075168/article/details/128605199