2022 Niuke winter vacation algorithm basic training camp 2 ACEFHIK (remaining to be supplemented)

Posted by wpt394 on Thu, 27 Jan 2022 03:24:35 +0100

A. Small sand furnace stone

Link: https://ac.nowcoder.com/acm/contest/23477/A
Source: niuke.com

Title Description

Xiao Sha is keen on dueling. Today, he and his younger brother played Firestone. His younger brother had a special dish, but in order to take care of his younger brother's self-esteem, Xiao Sha wanted to kill his younger brother.

Just kill: your brother's blood volume just becomes 0.

Xiaosha currently has nn spell attack cards in his hand. Each card will consume a little mana and cause a little basic damage. There are mm spell recovery cards, which do not need to consume mana value and can recover a little mana at a time. Xiaosha had a little mana at the beginning, and there was no upper limit.

They are all spells.

There is an attendant on the small sand field. He can cause you to increase your spell damage by + 1 after casting a spell.

The damage of each spell attack card is equal to spell damage + basic damage.

Spell damage is initially 0.

You can't use attack spell cards against this follower.

The entourage cannot attack.

Now Xiaosha wants to ask you whether Xiaosha can just kill his brother now.

Enter Description:

Enter two numbers 1 in the first line≤n≤109,0≤m≤1091≤n≤109,0≤m≤109 Represent the spell attack card and spell recovery card of Xiaosha's hand respectively.
Enter a 1 on the second line≤k≤1051≤k≤105 On behalf of xiaoshayou kk Second inquiry
 subsequently kk Enter an integer 1 for each line≤x≤10181≤x≤1018 Represents the blood volume of my brother

Output Description:

For each query of Xiaosha, a line of string is returned
 If you can kill your brother“ YES"((without quotation marks)
Otherwise output“ NO"((without quotation marks)

Example 1

input

copy

2 1
3
1
4
6

output

copy

YES
YES
NO

It's a clever thinking problem. I understood the wrong meaning of the problem during the competition and wasted half an hour writing the wrong two points

First of all, we can find that we can control the order of playing cards, so as to adjust the damage caused. If you want to cause the most damage, you need to first drop m recovery cards, and then drop min(n, m + 1) attack cards (the number of attacks is constrained by N and m); If you want to cause the least damage, you need to attack and recover once, and then attack and recover again (note that the attack will also rise when recovering, so each attack is an equal difference sequence with a tolerance of 2)

Let \ (a = min(n, m + 1) \) be set, and the attack range in the case of a attack can be obtained from the summation formula of the arithmetic sequence: \ ([\ frac{(1+(2\times a-1))\times a}{2},a+\frac{(m+m+a-1)\times a}{2}] \). It can be proved that after limiting the number of attacks to a, any damage value in this section can be reached in most cases.

The above is the case where the fixed number of attacks is min(n, m + 1). In fact, in order to achieve perfect killing, it is not necessary to use up all the attack cards in your hand. According to the given X and the above formula, the candidate value of the number of attacks to achieve perfect killing can be determined. Because the left endpoint of the above attack range is simplified to \ (a^2 \), so that \ (a^2=x \) gets \ (a=\lfloor sqrt(x)\rfloor \), which is the number of attacks required. It can be verified that if you attack a+1 times, the left end point of the final damage interval must be greater than x, and it is impossible to cause perfect kill. At this time, it is also necessary to verify whether the right end of the interval is greater than or equal to X. directly bring the candidate value \ (a=\lfloor sqrt(x)\rfloor \) calculated above into the right end expression \ (a+\frac{(m+m+a-1)\times a}{2} \), and judge whether it is greater than or equal to X.

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
ll n, m, k, x;
signed main() {
	cin >> n >> m;
	cin >> k;
	for(int i = 1; i <= k; i++) {
		ll x;
		cin >> x;
		ll atknum = min(n, m + 1);
		ll s = (ll) sqrt(x);
		atknum = min(atknum, s);
		if(atknum + (m + m + atknum - 1) * atknum / 2 >= x) puts("YES");
		else puts("NO");
	}
	return 0;
}

B. Xiaosha's kill

Link: https://ac.nowcoder.com/acm/contest/23477/C
Source: niuke.com

Title Description

Xiaosha especially likes to kill the ball (he likes to kill as well as vegetables). In addition, Xiaosha is an individual who can be strange, so he often likes to kill. Xiaosha can only kill the high-range ball in the back court. If it is a small ball in the front court, there is no way to kill the ball. Today, Xiaosha and his doubles partner train to kill the ball together. We assume that Xiaosha's physical fitness is X at the beginning and consumes a point every time, b points will be recovered every time the ball is not killed. Here is a 01 sequence, which represents whether the ball played by the partner is a small ball or a high-range ball (0 represents a small ball and 1 represents a high-range ball). How many balls can Xiaosha kill at most in this game. Xiaosha's physical fitness cannot be negative.

Enter Description:

The first line contains three integers 0≤x,a,b≤1090≤x,a,b≤109 Each integer is separated by a space
 The 01 string in the second line (the length of the string is no more than 106106) represents whether the partner is playing a small ball or a high ball.

Output Description:

Output the maximum number of kills of Xiaosha

Example 1

input

copy

10 2 1
111111

output

copy

5

explain

We can choose to kill the ball for the second, third, fourth, fifth and sixth time, up to five times

Pure sign in. It doesn't matter if you don't kill the high ball.

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#define ll long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
ll x, a, b;
int main() {
	cin >> x >> a >> b;
	string s;
	cin >> s;
	int ans = 0;
	for(auto c : s) {
		if(c == '0') x += b;
		else {
			if(x >= a) x -= a, ans++;
			else x += b;
		}	
	}
	cout << ans;
	return 0;
}

E. The long road of Xiaosha

Link: https://ac.nowcoder.com/acm/contest/23477/E
Source: niuke.com

Title Description

Xiaosha has an n-point Complete graph (I don't know the definition of points). You can choose the direction for each edge and specify that each edge can only go once. May I ask the longest path of a complete graph with n points? Now Xiaosha wants to know what its minimum and maximum values are respectively?

Enter Description:

Enter a positive integer n≤109n≤109

Output Description:

In line output n For a complete graph of points, the minimum and maximum values of its longest path are separated by spaces

Example 1

input

copy

3

output

copy

2 3

The maximum value is easy to handle. If there are odd points, then the degree of each point is even. Obviously, you can find an Euler loop (including n(n-1)/2 edges); If there are even numbers of points and Euler's path needs two odd degree points and several even degree vertices, then in fact, n / 2 - 1 edges can be selectively removed, so that the original two odd degree vertices and n - 2 even degree vertices can be found to meet the longest requirement (it will not be strictly proved).

The minimum value can be considered in this way, because it is constructed first and then traversed. You might as well start from the starting point p0 to any direction, and set the point to p1. At this time, the edge direction from p0 to p1 has been determined, and then point the remaining edges associated with p1 to p1 (block the road as much as possible); In this way, we can only expand backward, point the edges associated with p0 to p0, find a point p2 from the points associated with these edges, and then point the remaining edges associated with p2 to p2 In this way, it can be found that the minimum value is n - 1.

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#define ll long long
#define int long long
#define pb push_back
#define pii pair<int,int>
using namespace std;

signed main() {
	ll n;
	cin >> n;
	ll mn, mx;
	mn = n - 1;
	if(n & 1) {
		mx = (n - 1) * n / 2;
	} else {
		mx = (n - 1) * n / 2 - (n / 2 - 1);
	}
	cout << mn << " " << mx;
	return 0;
}

F. Calculation of small sand

Link: https://ac.nowcoder.com/acm/contest/23477/F
Source: niuke.com

Title Description

Xiao Sha can't count, so he wants to start learning mathematics in primary school. Today, the teacher assigned a lot of arithmetic problems, but Xiao Sha, who likes to play tricks, found that in so many arithmetic problems, the symbols among them are + + or ×× And each question + + and ×× The positions are the same

Xiaosha wants to play quickly, so he asks his good brother to help him calculate the answer quickly.

Because the answer number is too large, we take the module of 1000000071000000007

Enter Description:

The first line gives two numbers n Representative has n Calculate the number q Representative has q Second inquiry
(2≤n≤1062≤n≤106,1≤q≤1051≤q≤105)
The second line gives a length of n-1 of*+What is the symbol of the string that represents the calculation we want to perform
 Given in the third line n An integer representing the number at each position
 subsequently q Two numbers per line x,y

Represents the second x Change the first number to y
 And x≤nx≤n
 All numbers in this question are positive integers and less than 1000000071000000007
 However, there is no guarantee that there will be a value greater than 1000000071000000007 in the calculation process

Output Description:

For each query, answer the answer of the whole formula, and each answer occupies one line
(Each query in this question is not independent, that is, the number after each query modification will be modified forever)

Example 1

input

copy

5 3
++*+
1 1 3 1 1
4 2
1 7
5 6

output

copy

9
15
20

The data structure is used to maintain multiple modification operations. It is not difficult to find out by analyzing the problem that the continuous addition segment and the continuous multiplication segment can be separated to maintain their operation results. At the same time, map is used to maintain which addition segment and which multiplication segment each position corresponds to. When modifying, only the corresponding addition segment / multiplication segment needs to be modified. It should be noted that during the maintenance of multiplication section, the division should be realized by inverse element. There are many details. See the code for details.

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#define mod 1000000007
#define ll long long
#define int long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
int n, q;
string s;
int a[1000005];
int totans = 0;
ll fpow(ll a, ll b) {
	ll ans = 1;
	ll res = a % mod;
	while(b > 0) {
		if(b & 1) ans = ans * res % mod;
		b >>= 1;
		res = res * res % mod;
	}
	return ans;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n >> q;
	cin >> s;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int cnt = 0, lst = 0;
	vector<pair<int, int> > v2;
	vector<int> v;
	map<int, pair<int, bool> > mp;
	s = s + '+';
	for(int i = 0; i < s.size(); i++) {
		if(s[i] == '*') {
			if(!cnt) {
				cnt = 1;
				lst = i;
			} else cnt++;
		} else {
			if(i > 0) {
				if(s[i - 1] == '*') {
					v2.pb(make_pair(lst, i - 1));
					lst = i + 1;
					cnt = 0;
				}
			}
		}
	}
	lst = 1;
	for(int i = 0; i < v2.size(); i++) {
		v2[i].first += 1;
		v2[i].second += 2;
		ll ans = 1;
		int now = v.size();
		for(int j = v2[i].first; j <= v2[i].second; j++) {
			ans = ans * a[j] % mod;
			mp[j] = make_pair(now, 1);//1 stands for multiply
		}
		v.pb(ans);
		totans = (totans + ans) % mod;
		if(v2[i].first - 1 >= lst && (i == 0 || v2[i - 1].second + 1 != v2[i].first)) {
			ll ans = 0;
			int now = v.size();
			for(int j = lst; j <= v2[i].first - 1; j++) {
				ans = (ans + a[j]) % mod;
				mp[j] = make_pair(now, 0);//0 stands for plus
			}
			v.pb(ans);
			totans = (totans + ans) % mod;
			
		}
		lst = v2[i].second + 1;//It cannot be placed in the if above, or it may not be updated
	}
	if(lst <= n) {
		ll ans = 0;
		int now = v.size();
		for(int j = lst; j <= n; j++) {
			ans = (ans + a[j]) % mod;
			mp[j] = make_pair(now, 0);//0 stands for plus
		}
		v.pb(ans);
		totans = (totans + ans) % mod;
	}
	for(int i = 1; i <= q; i++) {
		int x, y;
		cin >> x >> y;
		totans = (totans - v[mp[x].first] + mod) % mod;
		if(mp[x].second == 0) {
			v[mp[x].first] = (v[mp[x].first] - a[x] + mod) % mod;
			v[mp[x].first] = (v[mp[x].first] + y) % mod;
			a[x] = y;
		} else {
			v[mp[x].first] = v[mp[x].first] * fpow(a[x], mod - 2) % mod;
			v[mp[x].first] = v[mp[x].first] * y % mod;
			a[x] = y;
		}
		totans = (v[mp[x].first] + totans) % mod;
		cout << totans << endl;
	}
}

H. Number of small sand

Link: https://ac.nowcoder.com/acm/contest/23477/H
Source: niuke.com

Title Description

There is an array of A. We know that its length is n and the sum of a [+] is m. if we want the maximum value of a [⊕], how many cases does array a have when a[+]=m
We define a [+] as a1 + A2 ana1​+a2​.... Value of an +
So a [⊕] refers to A1 ⊕ a2a2 ⊕ a3a3 an.... The value of an , where array A is all nonnegative integers.
The answer is 1e9+7 XOR definition (just order this if you don't understand)

Enter Description:

Enter two integers 1≤n≤1018,0≤m≤10181≤n≤1018,0≤m≤1018

Output Description:

Output an integer to represent the number of schemes

Example 1

input

copy

3 1

output

copy

3

Consider it bit by bit from high to low. Because a[+]=m is required, the highest 1 of each a must not exceed the highest 1 of M, and XOR and maximum are required. For all a, there must be and only one a, one 1 is located at the position of the highest 1 of M, and the other a is 0. Similarly, if the m-th high order is 1, it is the same processing method. If it is 0, this bit of all a must be 0 (otherwise, the sum must exceed m). In this way, we can know that the answer is actually \ (n^{count(m)} \), where count(m) represents the number of 1 in the binary representation of M, that is, for each binary bit of 1, the bit of only one a in N A is 1; For each binary bit of 0, this bit of all a is 0, which can be calculated according to the multiplication principle.

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#define mod (1000000000+7)
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
ll n, m, mm;
ll fpow(ll a, ll b) {
	ll ans = 1;
	ll res = a % mod;
	while(b > 0) {
		if(b & 1) ans = ans * res % mod;
		b >>= 1;
		res = res * res % mod;
	}
	return ans;
}
ll countBits(ll n) {
    ll count = 0;
    while(n != 0) {
        n = n & (n-1);
        count++;
    }
    return count;
}
signed main() {
	cin >> n >> m;
	mm = m;
	ll cnt = countBits(mm);
	if(cnt == 1) {
		cout << n % mod;
	} else {
		cout << fpow(n, cnt) % mod;
	}
	return 0;
}

1. Structure of small sand

Link: https://ac.nowcoder.com/acm/contest/23477/I
Source: niuke.com

Title Description

Xiaosha's structure problem is gone. He is very sad, so he wants to give you this structure, and what you need is to speed up your hand and seize the blood of this problem.

This question Xiaosha wants you to construct a string. This string has the following characteristics.

1. He is a symmetric string, and he has vertical symmetry about this string. For example, "()", if we turn him over, he is also "()", for example, "p" turns over to "q".

2. All characters in this string are visible characters except lowercase letters (excluding spaces).

Now Xiaosha wants you to construct a string with a length of nn and a different number of characters of mm.

The visible characters are as follows:

!"#$%&'()*+,-./0123456789:;<=>?@[]^_`QWERTYUIOPASDFGHJKLZXCVBNM{}|~

Among them, those with symmetric properties are

"!'*+-.08:=^_WTYUIOAHXVM|<>/[]{}()

Enter Description:

Enter two integers 1 in the first line≤n≤104,1≤m≤361≤n≤104,1≤m≤36. 

Output Description:

If such a string can be constructed, the string will be output
 Otherwise output-1

Example 1

input

copy

3 1

output

copy

OOO

Example 2

input

copy

3 3

output

copy

<=>

Disgusting string construction. Firstly, the given characters are divided into two categories, one is self symmetric, and the other is two combined symmetric. When constructing, first use the combined symmetry, and then use the self symmetry (otherwise it is difficult to adjust). Too many details, see the code oo

Pay attention to the situation of special JUDGMENT-1 (m=36 comparison pit, directly output - 1, and see the code for other situations).

#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#define ll long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
char c1[66] = {"\"!'*+-.08:=^_WTYUIOAHXVM|"};
char c2[66][3] = {{"<>"}, {"\\/"}, {"[]"}, {"{}"}, {"()"}};
int b[255] = { 0 };
int main() {
	int n, m;
	cin >> n >> m;
	int len1 = 25, len2 = 5;
	string ans1 = "", ans2 = "";
	if(n < m || m == 36) {
		puts("-1");
		return 0;
	}
	int pos = 0, pos2 = 0;
	for(int i = 0; i < n / 2; i++) {
		if(m) {
			if(pos2 < 5 && m > 2) {//It must be m > 2, otherwise it can't pass the examples of 11 and 10
				ans1 += c2[pos2][0];
				ans2 = c2[pos2][1] + ans2;
				pos2++;
				m -= 2;
			} else {
				ans1 += c1[pos];
				ans2 = c1[pos] + ans2;
				pos++;
				m--;
			}
		} else {
			ans1 = ans1 + c1[0];
			ans2 = c1[0] + ans2;
		}
	}
	string ans = "";
	if(n & 1) {
		if(m) {
			if(pos != 25) {
				ans1 += c1[pos];
				m--;
			}
		} else {
			ans1 += c1[0];
		}
	}
	ans = ans1 + ans2;
	if(m) {
		puts("-1");
	} else {
		cout << ans;
	}
	return 0;
}
//11 10
//5 5
//12 10
//13 12
//14 13
//35 2

K. The pace of little sand

Pure sign in, skip