Codeforces Round #751 (Div. 2)(A - C)

Keywords: C C++ Algorithm CodeForces

A.Two Subsequences

Topic Essentials

Give you a string s. You need to find two non-empty strings a and b to satisfy the following conditions:
(1) Strings a and b are subsequences of s.
(2) For each index i, the character s [i] in the string s must exactly belong to one of the strings a or b.
(3) String a is the smallest possible dictionary order; String b can be any possible string.
For a given string s, print any valid strings a and b.

Solving problems

Because the title requires the minimum dictionary order of string a, we can make string a the smallest character in string s, and string b the string s to delete string a.

AC Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		string s, c;
		cin >> s;
		c = s;
		sort(c.begin(), c.end()); //Sort string c
		int k = s.find(c[0]); //Find the subscript for the smallest character in string s
		s.erase(s.begin() + k); //Delete the character at that position in string s
		cout << c[0] << ' ' << s << endl; //Output a and b
	}
	return 0;
}

B. Divine Array

Topic Essentials

Give you a n array a whose length is n, and array a can be manipulated countless times.
For each operation, at each position J in the array, a j becomes equal to the number of times that a j appears in array a before this operation.
There are m queries asking, what is the value of the Xth number after k operations?

Solving problems

Based on the simulation of several examples, it is found that after an array passes a certain operand, the value of the array does not change and n<=2000, so we might as well calculate the values of the array from 1 to n operations without timeout. (Proved, at most log ⁡ 2 ( n ) \log_2(n) The value of the array will not change after log2 (n) operations)

AC Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2010;
int cnt[N], d[N][N]; //The cnt array records the number of times each array appears in the array						
int main()           //d[i][j] denotes the value of the jth position after the ith operation
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &d[0][i]);
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++) cnt[j] = 0; //Initialize cnt array before each operation
			for (int j = 1; j <= n; j++) cnt[d[i - 1][j]]++; //Update cnt array
			for (int j = 1; j <= n; j++) d[i][j] = cnt[d[i - 1][j]]; //Modify the value after the first operation
		}
		int m;
		scanf("%d", &m);
		while (m--)
		{
			int k, x;
			scanf("%d %d", &x, &k);
			printf("%d\n", d[min(k, n)][x]);
		}
	}
	return 0;
}

C. Array Elimination

Topic Essentials

Give you n numbers, you can choose any number of K (1<=k<=n) to operate on any number of times, assign the result of this number to x, subtract the number of K from x, and eventually make the number of N zero to find out how many such K are.

Solving problems

First, we need to know that when both numbers are 1, &together results in 1.
Secondly, the title requires that the result of N numbers be zero at the end. It is possible to deduce that each of the binary representations of the last n numbers is zero. Then, how to change the number of n to the number of zeros i n the first k, the number of first k must all be 1, and the number of second I must be an integer multiple of k i n order to make all 1 i n the first I become 0. Finally, how to make every one zero, find out the number of each one, and find out the maximum common factor res of these numbers. So K is all the factors of res and is less than or equal to n.

AC Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int p[N], cnt[32]; //cnt[i] record number of position 1
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
		memset(cnt, 0, sizeof(cnt)); //Initialize cnt array
		for (int i = 1; i <= n; i++) //Traversal n Numbers
		{
			for (int j = 0; j < 30; j++) //Traverse every bit of every number
			{
				int k = (p[i] >> j) & 1;
				cnt[j] += k;
			}
		}
		int res = 0;
		for (int i = 0; i < 30; i++) res = gcd(res, cnt[i]);
		for (int i = 1; i <= n; i++)
		{
			if (res % i == 0) printf("%d ", i);
		}
		puts("");
	}
	return 0;
}

Posted by rash on Sun, 31 Oct 2021 11:33:25 -0700