Codeforces Round 921 (Div. 2 ABCDE题)

A. We Got Everything Covered!

Problem Statement

You are given two positive integers n n n and k k k.

Your task is to find a string s s s such that all possible strings of length n n n that can be formed using the first k k k lowercase English alphabets occur as a subsequence of s s s.

If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.

Note: A string a a a is called a subsequence of another string b b b if a a a can be obtained by deleting some (possibly zero) characters from b b b without changing the order of the remaining characters.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 676 1\leq t\leq 676 1t676) denoting the number of test cases.

Each test case consists of a single line of input containing two integers n n n ( 1 ≤ n ≤ 26 1\leq n\leq 26 1n26) and k k k ( 1 ≤ k ≤ 26 1\leq k\leq 26 1k26).

Output

For each test case, print a single line containing a single string s s s which satisfies the above property. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.

Example

Example

input
4
1 2
2 1
2 2
2 3
output
ab
aa
baab
abcbac

Solution

具体见文后视频。


Code

#include 
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int N, K;

	cin >> N >> K;

	for (int i = 1; i <= N; i ++)
		for (char j = 'a'; j <= 'a' + K - 1; j ++)
			cout << j;
	cout << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}

B. A Balanced Problemset?

Problem Statement

Jay managed to create a problem of difficulty x x x and decided to make it the second problem for Codeforces Round #921.

But Yash fears that this problem will make the contest highly unbalanced, and the coordinator will reject it. So, he decided to break it up into a problemset of n n n sub-problems such that the difficulties of all the sub-problems are a positive integer and their sum is equal to x x x.

The coordinator, Aleksey, defines the balance of a problemset as the GCD of the difficulties of all sub-problems in the problemset.

Find the maximum balance that Yash can achieve if he chooses the difficulties of the sub-problems optimally.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 1\leq t\leq 10^3 1t103) denoting the number of test cases.

Each test case contains a single line of input containing two integers x x x ( 1 ≤ x ≤ 1 0 8 1\leq x\leq 10^8 1x108) and n n n ( 1 ≤ n ≤ x 1\leq n\leq x 1nx).

Output

For each test case, print a single line containing a single integer denoting the maximum balance of the problemset Yash can achieve.

Example

Example

input
3
10 3
5 5
420 69
output
2
1
6

Solution

具体见文后视频。


Code

#include 
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int X, N;

	cin >> X >> N;

	std::vector<PII> Factor;
	for (int i = 1; i <= X / i; i ++)
		if (X % i == 0)
			Factor.push_back({i, X / i});

	int Result = 0;
	for (auto c : Factor)
	{
		if (c.second >= N)
			Result = max(Result, c.first);
		if (c.first >= N)
			Result = max(Result, c.second);
	}

	cout << Result << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}

C. Did We Get Everything Covered?

Problem Statement

You are given two integers n n n and k k k along with a string s s s.

Your task is to check whether all possible strings of length n n n that can be formed using the first k k k lowercase English alphabets occur as a subsequence of s s s. If the answer is NO, you also need to print a string of length n n n that can be formed using the first k k k lowercase English alphabets which does not occur as a subsequence of s s s.

If there are multiple answers, you may print any of them.

Note: A string a a a is called a subsequence of another string b b b if a a a can be obtained by deleting some (possibly zero) characters from b b b without changing the order of the remaining characters.

Input

The first line of input contains a single integer t   ( 1 ≤ t ≤ 1 0 5 ) t \, (1 \le t \le 10^5) t(1t105), the number of test cases.

The first line of each test case contains 3 3 3 integers n   ( 1 ≤ n ≤ 26 ) ,   k   ( 1 ≤ k ≤ 26 ) ,   m   ( 1 ≤ m ≤ 1000 ) n \, (1 \le n \le 26), \: k \, (1 \le k \le 26), \: m \, (1 \le m \le 1000) n(1n26),k(1k26),m(1m1000), where n n n and k k k are the same as described in the input and m m m is the length of the string s s s.

The second line of each test case contains a single string s s s of length m m m, comprising only of the first k k k lowercase English alphabets.

It is guaranteed that the sum of m m m and the sum of n n n over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case, print YES if all possible strings of length n n n that can be formed using the first k k k lowercase English alphabets occur as a subsequence of s s s, else print NO.

If your answer is NO, print a string of length n n n that can be formed using the first k k k lowercase English alphabets which does not occur as a subsequence of s s s in the next line.

You may print each letter of YES or NO in any case (for example, YES, yES, YeS will all be recognized as a positive answer).

Example

Example

input
3
2 2 4
abba
2 2 3
abb
3 3 10
aabbccabab
output
YES
NO
aa
NO
ccc

Solution

具体见文后视频。


Code

#include 
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

int N, K, M;
string S;

void solve()
{
	cin >> N >> K >> M >> S;

	S = ' ' + S;
	set<int> T;
	std::vector<char> Result;
	int Part = 0;
	for (int i = 1; i <= M; i ++)
	{
		T.insert(S[i] - 'a');
		if (T.size() == K)
		{
			Result.push_back(S[i]);
			T.clear(), Part ++;
		}
	}

	if (Part >= N)
	{
		cout << "YES" << endl;
		return;
	}

	T.insert(28);
	cout << "NO" << endl;
	for (auto c : Result)
		cout << c;
	int Last = -1;
	for (auto c : T)
		if (Last + 1 < c)
		{
			cout << char('a' + Last + 1);
			break;
		}
		else
			Last = c;

	for (int i = 1; i <= N - Part - 1; i ++)
		cout << 'a';
	cout << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}

D. Good Trip

Problem Statement

There are n n n children in a class, m m m pairs among them are friends. The i i i-th pair who are friends have a friendship value of f i f_i fi.

The teacher has to go for k k k excursions, and for each of the excursions she chooses a pair of children randomly, equiprobably and independently. If a pair of children who are friends is chosen, their friendship value increases by 1 1 1 for all subsequent excursions (the teacher can choose a pair of children more than once). The friendship value of a pair who are not friends is considered 0 0 0, and it does not change for subsequent excursions.

Find the expected value of the sum of friendship values of all k k k pairs chosen for the excursions (at the time of being chosen). It can be shown that this answer can always be expressed as a fraction p q \dfrac{p}{q} qp where p p p and q q q are coprime integers. Calculate p ⋅ q − 1   m o d   ( 1 0 9 + 7 ) p\cdot q^{-1} \bmod (10^9+7) pq1mod(109+7).

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \le t \le 5 \cdot 10^4 1t5104). Description of the test cases follows.

The first line of each test case contains 3 3 3 integers n n n, m m m and k k k ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105, 0 ≤ m ≤ min ⁡ ( 1 0 5 0 \le m \le \min \Big(10^5 0mmin(105, $ \frac{n(n-1)}{2} \Big)$ , 1 ≤ k ≤ 2 ⋅ 1 0 5 1 \le k \le 2 \cdot 10^5 1k2105) — the number of children, pairs of friends and excursions respectively.

The next m m m lines contain three integers each — a i a_i ai, b i b_i bi, f i f_i fi — the indices of the pair of children who are friends and their friendship value. ( a i ≠ b i a_i \neq b_i ai=bi, 1 ≤ a i , b i ≤ n 1 \le a_i,b_i \le n 1ai,bin, 1 ≤ f i ≤ 1 0 9 1 \le f_i \le 10^9 1fi109). It is guaranteed that all pairs of friends are distinct.

It is guaranteed that the sum of n n n and sum m m m over all test cases does not exceed 1 0 5 10^5 105 and the sum of k k k over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, print one integer — the answer to the problem.

Example

Example

input
4
100 0 24
2 1 10
1 2 1
3 1 2
2 1 1
5 2 4
1 2 25
3 2 24
output
0
55
777777784
40000020

Solution

具体见文后视频。

Note

For the first test case, there are no pairs of friends, so the friendship value of all pairs is 0 0 0 and stays 0 0 0 for subsequent rounds, hence the friendship value for all excursions is 0 0 0.

For the second test case, there is only one pair possible ( 1 , 2 ) (1, 2) (1,2) and its friendship value is initially 1 1 1, so each turn they are picked and their friendship value increases by 1 1 1. Therefore, the total sum is 1 + 2 + 3 + … + 10 = 55 1+2+3+\ldots+10 = 55 1+2+3++10=55.

For the third test case, the final answer is 7 9 = 777   777   784   m o d   ( 1 0 9 + 7 ) \frac{7}{9} = 777\,777\,784\bmod (10^9+7) 97=777777784mod(109+7).

Code

#include 
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int MOD = 1e9 + 7;

int N, M, K;

int Quick_Pow(int a, int b, int p)
{
	int Result = 1;
	while (b)
	{
		if (b & 1) Result = Result * a % p;
		a = a * a % p;
		b >>= 1;
	}

	return Result;
}

void solve()
{
	cin >> N >> M >> K;

	int A, B, F, S = 0;
	for (int i = 1; i <= M; i ++)
		cin >> A >> B >> F, S = (S + F) % MOD;

	int Result = 0, Inv2 = Quick_Pow(2, MOD - 2, MOD);
	for (int i = 1; i <= K; i ++)
	{
		Result = (Result + S * Quick_Pow(N * (N - 1) % MOD * Inv2 % MOD, MOD - 2, MOD) % MOD) % MOD;
		S = (S + M * Quick_Pow(N * (N - 1) % MOD * Inv2 % MOD, MOD - 2, MOD) % MOD) % MOD;
	}

	cout << Result << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}

E. Space Harbour

Problem Statement

There are n n n points numbered 1 1 1 to n n n on a straight line. Initially, there are m m m harbours. The i i i-th harbour is at point X i X_i Xi and has a value V i V_i Vi. It is guaranteed that there are harbours at the points 1 1 1 and n n n. There is exactly one ship on each of the n n n points. The cost of moving a ship from its current location to the next harbour is the product of the value of the nearest harbour to its left and the distance from the nearest harbour to its right. Specifically, if a ship is already at a harbour, the cost of moving it to the next harbour is 0 0 0.

Additionally, there are q q q queries, each of which is either of the following 2 2 2 types:

  • 1 1 1 x x x v v v — Add a harbour at point x x x with value v v v. It is guaranteed that before adding the harbour, there is no harbour at point x x x.
  • 2 2 2 l l l r r r — Print the sum of the cost of moving all ships at points from l l l to r r r to their next harbours. Note that you just need to calculate the cost of moving the ships but not actually move them.

Input

The first line contains three integers n n n, m m m, and q q q ( 2 ≤ m ≤ n ≤ 3 ⋅ 1 0 5 2 \le m \le n \le 3 \cdot 10^5 2mn3105, 1 ≤ q ≤ 3 ⋅ 1 0 5 1 \le q \le 3 \cdot 10^5 1q3105) — the number of points, harbours, and queries, respectively.

The second line contains m m m distinct integers X 1 , X 2 , … , X m ( 1 ≤ X i ≤ n ) X_1, X_2, \ldots, X_m(1 \le X_i \le n) X1,X2,,Xm(1Xin) — the position at which the i i i-th harbour is located.

The third line contains m m m integers V 1 , V 2 , … , V m ( 1 ≤ V i ≤ 1 0 7 ) V_1, V_2, \ldots, V_m(1 \le V_i \le 10^7) V1,V2,,Vm(1Vi107) — the value of the i i i-th harbour.

Each of the next q q q lines contains three integers. The first integer is t t t ( 1 ≤ t ≤ 2 1\le t \le 2 1t2) — type of query. If t = 1 t=1 t=1, then the next two integers are x x x and v v v ( 2 ≤ x ≤ n − 1 2 \le x \le n - 1 2xn1, 1 ≤ v ≤ 1 0 7 1 \le v \le 10^7 1v107) — first-type query. If t = 2 t=2 t=2, then the next two integers are l l l and r r r ( 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn) — second-type query.

It is guaranteed that there is at least one second-type query.

Output

For every second-type query, print one integer in a new line — answer to this query.

Example

Example

input
8 3 4
1 3 8
3 24 10
2 2 5
1 5 15
2 5 5
2 7 8
output
171
0
15

Solution

具体见文后视频。


Code

#include 
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int SIZE = 3e5 + 10;

int N, M, Q;
int X[SIZE], V[SIZE], Val[SIZE];
PII P[SIZE];

struct Segment
{
	struct Node
	{
		int l, r;
		LL Sum, Lazy;
	}Tree1[SIZE << 2];
	void Pushup(int u) { Tree1[u].Sum = Tree1[u << 1].Sum + Tree1[u << 1 | 1].Sum; }
	void Pushdown(int u)
	{
		if (Tree1[u].Lazy)
		{
			Tree1[u << 1].Sum += (LL)(Tree1[u << 1].r - Tree1[u << 1].l + 1) * Tree1[u].Lazy;
			Tree1[u << 1].Lazy += Tree1[u].Lazy;
			Tree1[u << 1 | 1].Sum += (LL)(Tree1[u << 1 | 1].r - Tree1[u << 1 | 1].l + 1) * Tree1[u].Lazy;
			Tree1[u << 1 | 1].Lazy += Tree1[u].Lazy;
			Tree1[u].Lazy = 0;
		}
	}
	void Build(int u, int l, int r)
	{
		Tree1[u] = {l, r};
		if (l == r) return;
		int mid = l + r >> 1;
		Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);
	}
	void Modify(int u, int l, int r, int d)
	{
		if (Tree1[u].l >= l && Tree1[u].r <= r)
		{
			Tree1[u].Sum += (LL)(Tree1[u].r - Tree1[u].l + 1) * d;
			Tree1[u].Lazy += d;
			return;
		}

		Pushdown(u);
		int mid = Tree1[u].l + Tree1[u].r >> 1;
		if (mid >= l) Modify(u << 1, l, r, d);
		if (mid < r) Modify(u << 1 | 1, l, r, d);
		Pushup(u);
	}
	long long Query(int u, int l, int r)
	{
		if (Tree1[u].l >= l && Tree1[u].r <= r)
			return Tree1[u].Sum;

		Pushdown(u);
		long long mid = Tree1[u].l + Tree1[u].r >> 1, Result = 0;
		if (mid >= l) Result = Query(u << 1, l, r);
		if (mid < r) Result += Query(u << 1 | 1, l, r);

		return Result;
	}
	struct Node2
	{
		int l, r;
		__int128 Sum, Lazy;
	}Tree2[SIZE << 2];
	void Pushup2(int u) { Tree2[u].Sum = Tree2[u << 1].Sum + Tree2[u << 1 | 1].Sum; }
	void Pushdown2(int u)
	{
		if (Tree2[u].Lazy)
		{
			int L = Tree2[u << 1].l, R = Tree2[u << 1].r, d = Tree2[u].Lazy;
			Tree2[u << 1].Sum += (__int128)(L + R - 2) * (R - L + 1) / 2 * d;
			Tree2[u << 1].Lazy += d;
			L = Tree2[u << 1 | 1].l, R = Tree2[u << 1 | 1].r, d = Tree2[u].Lazy;
			Tree2[u << 1 | 1].Sum += (__int128)(L + R - 2) * (R - L + 1) / 2 * d;
			Tree2[u << 1 | 1].Lazy += d;
			Tree2[u].Lazy = 0;
		}
	}
	void Build2(int u, int l, int r)
	{
		Tree2[u] = {l, r};
		if (l == r) return;
		int mid = l + r >> 1;
		Build2(u << 1, l, mid), Build2(u << 1 | 1, mid + 1, r);
	}
	void Modify2(int u, int l, int r, int d)
	{
		if (Tree2[u].l >= l && Tree2[u].r <= r)
		{
			int L = Tree2[u].l, R = Tree2[u].r;
			Tree2[u].Sum += (__int128)(L + R - 2) * (R - L + 1) / 2 * d;
			Tree2[u].Lazy += d;
			return;
		}

		Pushdown2(u);
		int mid = Tree2[u].l + Tree2[u].r >> 1;
		if (mid >= l) Modify2(u << 1, l, r, d);
		if (mid < r) Modify2(u << 1 | 1, l, r, d);
		Pushup2(u);
	}
	__int128 Query2(int u, int l, int r)
	{
		if (Tree2[u].l >= l && Tree2[u].r <= r)
			return Tree2[u].Sum;

		Pushdown2(u);
		__int128 mid = Tree2[u].l + Tree2[u].r >> 1, Result = 0;
		if (mid >= l) Result = Query2(u << 1, l, r);
		if (mid < r) Result += Query2(u << 1 | 1, l, r);
		return Result;
	}
}Tool;

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> N >> M >> Q;

	for (int i = 1; i <= M; i ++)
		cin >> P[i].first;
	for (int i = 1; i <= M; i ++)
		cin >> P[i].second;

	sort(P + 1, P + 1 + M);
	Tool.Build(1, 1, N);
	Tool.Build2(1, 1, N);

	for (int i = 1; i <= M; i ++)
		X[i] = P[i].first, V[i] = P[i].second;

	for (int i = 2; i <= M; i ++)
	{
		int Head = V[i - 1] * X[i] - V[i - 1] * (X[i - 1] + 1), D = -V[i - 1];
		Tool.Modify(1, X[i - 1] + 1, X[i - 1] + 1, Head);
		if (X[i - 1] + 2 <= X[i] - 1) Tool.Modify(1, X[i - 1] + 2, X[i] - 1, Head - X[i - 1] * D);
		if (X[i - 1] + 2 <= X[i] - 1) Tool.Modify2(1, X[i - 1] + 2, X[i] - 1, D);
	}

	set<int> S;
	for (int i = 1; i <= M; i ++)
		S.insert(X[i]), Val[X[i]] = V[i];
	while (Q --)
	{
		int t, l, r;

		cin >> t >> l >> r;

		if (t == 1)
		{
			int L = *(-- S.upper_bound(l)), R = *S.upper_bound(l);
			Tool.Modify(1, L + 1, l, -Val[L] * (R - l));
			int Head = R * (Val[L] - r) + (r - Val[L]) * (l + 1), D = r - Val[L];
			Tool.Modify(1, l + 1, l + 1, -Head);
			if (l + 2 <= R - 1) Tool.Modify(1, l + 2, R - 1, -(Head - l * D));
			if (l + 2 <= R - 1) Tool.Modify2(1, l + 2, R - 1, -D);
			S.insert(l), Val[l] = r;
		}
		else
		{
			int Result = Tool.Query(1, l, r) + (__int128)Tool.Query2(1, l, r);
			cout << Result << endl;
		}
	}

	return 0;
}

视频题解

Codeforces Round 921 (Div. 2)(A ~ E 题)


最后祝大家早日在这里插入图片描述

你可能感兴趣的:(Codeforces,c++,算法,Codeforces)