Summary of Group C++B in the second provincial match of the 11th Blue Bridge Cup 2020

2020/10/18 The
last Blue Bridge made a big deal . The fill-in-the-blank questions of this Blue Bridge are still quite happy. As a careless person, I feel the pleasure of filling in the blanks for the first time. The big questions are so-so. , The first question has nothing to say; the second question palindrome date is not clear about whether AAAAAAAA belongs to ABABBABA; the third question is the serial value of the string. . . The game was complacent with the complexity of O(nlogn*26), and it turned out that the big guy told me that it was O(n). Looking back at the data, it seems to be 1e6. I feel like it is going to roll over, I hope it is 1e5; the fourth question is not judged. On the important side, the correctness is unknown, and the examples are passed; the fifth question is written casually, and the examples are handed in; make a
wish and save one, hope to change the name! ! ! ! !
Not much nonsense, here only fill in the blanks, mainly to share questions and answers, the code is not original (mainly lazy), intrusion and deletion.
A.
Doorplate making Problem description: Calculate how many times 2 appears in 1-2020, and note that 2 does not appear in many numbers.
Idea: Calculate directly.
Code:

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    int cnt = 0;
    for (int i = 1; i <= 2020; i++) {
    
    
        int x = i;
        while (x) {
    
    
            if (x % 10 == 2) ++cnt;
            x /= 10;
        }
    }
    cout << cnt << "\n";
    return 0;
}

Answer: 624
B.
Reduced fractions Problem description: How many fractions should be found so that the greatest common divisor of the numerator and denominator is 1, and both the numerator and denominator are between 1 and 2020.
Idea: Directly judge gcd with two layers of for enumeration
Code:

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    int cnt = 0;
    for (int i = 1; i <= 2020; i++) {
    
    
        for (int j = 1; j <= 2020; j++) {
    
    
            if (__gcd(i, j) == 1) 
                ++cnt;
        }
    }
    cout << cnt << "\n";
    return 0;
}

Answer: 2481215
C. Snake-shaped fill in the number
Problem description: Specify a matrix as follows:
1 2 6 7
3 5 8
4 9
10
Ask you how many
ideas are in the 20th row and the 20th column : You can open an array, each time the hypotenuse order or Construct in reverse order.
Code:

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int x = 1, y = 1;
    int a[100][100] = {
    
    };
    int num = 1;
    for (int i = 1; x <= 50; i++) {
    
    
        for (int j = 0; j < i; j++) {
    
    
            a[x][y] = num++;
            if (j != i - 1) {
    
    
                if (i & 1) --x, ++y;
                else ++x, --y;
            }
        }
        if (i & 1) ++y;
        else ++x;
    }
    cout << a[20][20] << "\n";
    return 0;
}

Answer: 761
D.
Description of running problem: Run 2 kilometers on the first day of each month or on the first day of the week, and run 1 kilometers on the other days. I ask you from January 1, 2000 (inclusive) to October 2020 How many kilometers were run on the 1st (inclusive).
Idea: Simulate the number of days in a flat leap year, mark it as the day of the week, calculate how many days are 2 kilometers away, and finally use the number of days + the number of extra days calculated.
Code:

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
   ios::sync_with_stdio(false); \
   // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {
    
    1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int w=6;
bool rui(int n)
{
    
    
	if(n%400==0||(n%4==0&&n%100!=0))
	return true;
	return false;
}
int count(int year)
{
    
    
	int cnt=0;
	if(rui(year))
		m[2]+=1;
	if(year==2020)
	{
    
    
		for(int i=1;i<=9;i++)
		{
    
    
			for(int j=1;j<=m[i];j++)
			{
    
    
				if(w==1||j==1)			
				{
    
    
					cnt++;	
				}	
				w++;
				if(w==8)
				w=1;
			}
		}
	}
	else
	{
    
    
		for(int i=1;i<=12;i++)
		{
    
    
			for(int j=1;j<=m[i];j++)	
			{
    
    
				if(w==1||j==1)			
				{
    
    
					cnt++;	
				}
				w++;
				if(w==8)
				w=1;	 
			}
		}		
	}
	if(rui(year))
		m[2]-=1;
	return cnt;
}
int main()
{
    
    
   IO;
   int ans=0;
	for(int i=2000;i<=2020;i++)
	{
    
    
		ans+=count(i);	
	}
	cout<<ans+7580+1;// 还要加上10.1这天 
   return 0;
}

Answer: 8879
E. Seven-segment code
Description of the problem: Seven lights of a, b, c, d, e, f, g of the seven-segment digital tube, ask you how many characters can be expressed at most (the lights must be connected)
Insert picture description here

Idea: First dfs, for 7 lights, use 0, 1 to indicate whether the light is on or not. There are (2^7-1) states (all off is not counted), and judge the result of each dfs;
there are two kinds of judges method:

  1. Build a map, for example, a can go to b and f, and finally start to connect from a bright point to determine whether all points can be reached.
  2. And check the set, the adjacent lights are counted as a set, each time the parent node is initialized as itself, if one light is on, join the lights in the adjacent lights together, and finally judge all the lights that are on Whether the lights are all in one set.

Code:

#include <bits/stdc++.h>
using namespace std;

bool light[7];
vector<vector<int> > G(7);
int ans;

bool judge(vector<int> &v1) {
    
    
    vector<int> v2;
    queue<int> que;
    bool vis[7] = {
    
    };
    que.push(v1[0]);
    vis[v1[0]] = true;
    while (!que.empty()) {
    
    
        int u = que.front();
        que.pop();
        v2.push_back(u);
        for (int i = 0; i < G[u].size(); i++) {
    
    
            int v = G[u][i];
            if (find(v1.begin(), v1.end(), v) != v1.end() && !vis[v]) {
    
    
                que.push(v);
                vis[v] = true;
            }
        }
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    return v2 == v1;
}

void dfs(int dep) {
    
    
    if (dep == 7) {
    
    
        vector<int> v;
        for (int i = 0; i < 7; i++) if (light[i]) v.push_back(i);
        if (v.size() && judge(v)) ++ans;    
        return;
    }
    light[dep] = true;
    dfs(dep + 1);
    light[dep] = false;
    dfs(dep + 1);
}

void build_graph() {
    
    
    G[0].push_back(1), G[0].push_back(5);
    G[1].push_back(0), G[1].push_back(2), G[1].push_back(6);
    G[2].push_back(1), G[2].push_back(3), G[2].push_back(6);
    G[3].push_back(2), G[3].push_back(4);
    G[4].push_back(3), G[4].push_back(5), G[4].push_back(6);
    G[5].push_back(0), G[5].push_back(4), G[5].push_back(6);
    G[6].push_back(1), G[6].push_back(2), G[6].push_back(4), G[6].push_back(5);
}

int main() {
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    build_graph();
    dfs(0);
    cout << ans << "\n";
    return 0;
}

Answer: 80

Guess you like

Origin blog.csdn.net/ylwhxht/article/details/109145754