Codeforces Round #660 (Div. 2) A~D

A. Captain Flint and Crew Recruitment

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.

Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer xx as nearly prime if it can be represented as p⋅qp⋅q, where 1<p<q1<p<q and pp and qq are prime numbers. For example, integers 66 and 1010 are nearly primes (since 2⋅3=62⋅3=6 and 2⋅5=102⋅5=10), but integers 11, 33, 44, 1616, 1717 or 4444 are not.

Captain Flint guessed an integer nn and asked you: can you represent it as the sum of 44 different positive integers where at least 33 of them should be nearly prime.

Uncle Bogdan easily solved the task and joined the crew. Can you do the same?

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

Next tt lines contain test cases — one per line. The first and only line of each test case contains the single integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number Flint guessed.

Output

For each test case print:

  • YES and 44 different positive integers such that at least 33 of them are nearly prime and their sum is equal to nn (if there are multiple answers print any of them);
  • NO if there is no way to represent nn as the sum of 44 different positive integers where at least 33 of them are nearly prime.

You can print each character of YES or NO in any case.

Example

input

Copy

7
7
23
31
36
44
100
258

output

Copy

NO
NO
YES
14 10 6 1
YES
5 6 10 15
YES
6 7 10 21
YES
2 10 33 55
YES
10 21 221 6

Note

In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.

In the third test case, n=31=2⋅7+2⋅5+2⋅3+1n=31=2⋅7+2⋅5+2⋅3+1: integers 1414, 1010, 66 are nearly prime.

In the fourth test case, n=36=5+2⋅3+2⋅5+3⋅5n=36=5+2⋅3+2⋅5+3⋅5: integers 66, 1010, 1515 are nearly prime.

In the fifth test case, n=44=2⋅3+7+2⋅5+3⋅7n=44=2⋅3+7+2⋅5+3⋅7: integers 66, 1010, 2121 are nearly prime.

In the sixth test case, n=100=2+2⋅5+3⋅11+5⋅11n=100=2+2⋅5+3⋅11+5⋅11: integers 1010, 3333, 5555 are nearly prime.

In the seventh test case, n=258=2⋅5+3⋅7+13⋅17+2⋅3n=258=2⋅5+3⋅7+13⋅17+2⋅3: integers 1010, 2121, 221221, 66 are nearly prime.

Main idea:

Enter a number N and output four different numbers, three of which are approximate prime numbers (the product of two prime numbers), so that the sum of these four numbers is equal to N.

solution:

Find the first three approximate prime numbers, namely 6, 10, 14, and see if N minus these three numbers is greater than 0, and then judge whether the subtracted number is equal to one of them, if so, put 14 Change to 15, and subtract the remaining number -1.

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }



int main()
{
	int T; cin >> T;
	while (T--) {
		int n;
		sc("%d", &n);
		if (n <= 30)
			puts("NO");
		else {
			puts("YES");
			int w = n - 30;
			if (w == 6 || w == 10 || w == 14)
				printf("6 10 15 %d\n", w - 1);
			else
				printf("6 10 14 %d\n", w);
		}
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

B. Captain Flint and a Long Voyage

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.

In the beginning, uncle Bogdan wrote on a board a positive integer xx consisting of nn digits. After that, he wiped out xx and wrote integer kk instead, which was the concatenation of binary representations of digits xx consists of (without leading zeroes). For example, let x=729x=729, then k=111101001k=111101001 (since 7=1117=111, 2=102=10, 9=10019=1001).

After some time, uncle Bogdan understood that he doesn't know what to do with kk and asked Denis to help. Denis decided to wipe last nn digits of kk and named the new number as rr.

As a result, Denis proposed to find such integer xx of length nn that rr (as number) is maximum possible. If there are multiple valid xx then Denis is interested in the minimum one.

All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?

Note: in this task, we compare integers (xx or kk) as numbers (despite what representations they are written in), so 729<1999729<1999 or 111<1000111<1000.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

Next tt lines contain test cases — one per test case. The one and only line of each test case contains the single integer nn (1≤n≤1051≤n≤105) — the length of the integer xx you need to find.

It's guaranteed that the sum of nn from all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, print the minimum integer xx of length nn such that obtained by Denis number rr is maximum possible.

Example

input

Copy

2
1
3

output

Copy

8
998

Note

In the second test case (with n=3n=3), if uncle Bogdan had x=998x=998 then k=100110011000k=100110011000. Denis (by wiping last n=3n=3 digits) will obtain r=100110011r=100110011.

It can be proved that the 100110011100110011 is the maximum possible rr Denis can obtain and 998998 is the minimum xx to obtain it.

Main idea:

Input a number N, you have to construct a number whose digits are N, remove the last N of its binary number, and the remaining binary string should be as large as possible. If there are multiple such numbers, output the smallest one.

solution:

There are only 8 and 9 in four binary numbers, so use 8, 9 to construct, and see how many four digits are occupied by the removed N binary numbers. Fill in the front with 9 and the back with 8.

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }



int main()
{
	int T; cin >> T;
	while (T--) {
		int n;
		sc("%d", &n);
		int t = (n - 1) / 4 + 1;
		for (int i = 0; i < n - t; i++)
			printf("9");
		for (int i = 0; i < t; i++)
			printf("8");
		puts("");
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

C. Uncle Bogdan and Country Happiness

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.

There are nn cities and n−1n−1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 11 to nn and the city 11 is a capital. In other words, the country has a tree structure.

There are mm citizens living in the country. A pipi people live in the ii-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.

Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.

Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the ii-th city calculates a happiness index hihi as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.

Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.

Uncle Bogdan successfully solved the problem. Can you do the same?

More formally, You need to check: "Is it possible that, after all people return home, for each city ii the happiness index will be equal exactly to hihi".

Input

The first line contains a single integer tt (1≤t≤100001≤t≤10000) — the number of test cases.

The first line of each test case contains two integers nn and mm (1≤n≤1051≤n≤105; 0≤m≤1090≤m≤109) — the number of cities and citizens.

The second line of each test case contains nn integers p1,p2,…,pnp1,p2,…,pn (0≤pi≤m0≤pi≤m; p1+p2+…+pn=mp1+p2+…+pn=m), where pipi is the number of people living in the ii-th city.

The third line contains nn integers h1,h2,…,hnh1,h2,…,hn (−109≤hi≤109−109≤hi≤109), where hihi is the calculated happiness index of the ii-th city.

Next n−1n−1 lines contain description of the roads, one per line. Each line contains two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n; xi≠yixi≠yi), where xixi and yiyi are cities connected by the ii-th road.

It's guaranteed that the sum of nn from all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, print YES, if the collected data is correct, or NO — otherwise. You can print characters in YES or NO in any case.

Examples

input

Copy

2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5

output

Copy

YES
YES

input

Copy

2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3

output

Copy

NO
NO

Main idea:

 A tree of N points, the first weight of each point indicates how many residents live here (maybe not), everyone is initially at 1, and they have to walk from 1 to where they live. At the beginning, everyone either The mood is good or bad. On the way home, the mood can only go from good -> bad. The second weight of each point indicates that the person who is in a good mood-a person in a bad mood after that point goes home, the output is Whether the conditions are legal.

solution:

Knowing the size of the resident in the subtree of each point and the weight of the point, then the situation is unique, and it is enough to judge whether it is legal when DFS backtracks. There are two main things to judge: 1. Whether the residents are enough; 2. Whether the number of residents in the current subtree can be constructed into the weight of the point. When constructing, notice that the bad mood in the subtree can be turned into a good mood at the current point.

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

vector <int> G[N];
ll a[N], h[N];
ll good[N], bad[N]; 
int n, m;
bool ok;

void Init() {
	for (int i = 1; i <= n; i++) {
		G[i].clear();
		good[i] = bad[i] = 0;
	}
	ok = true;
}
void DFS(int x, int fa) {
	ll gd = 0, bd = 0;
	ll son = 0;

	for (auto v : G[x]) {
		if (v == fa)
			continue;
		DFS(v, x);
		son += a[v];   // 子树居民数
		gd += good[v];  // 子树好心情
		bd += bad[v];   // 子树坏心情
	}

	if (abs(h[x]) > a[x] + son)  // 数量不够
		ok = false;

	ll gd1 = (h[x] + son + a[x]) / 2;   // 当前情况的好心情数量
	ll bd1 = gd1 - h[x];                 // 坏心情

	if (gd + bd + a[x]!= gd1 + bd1 || gd1 < 0 || bd1 < 0 || gd > gd1) // 构造不了
		ok = false;
	bad[x] = bd1, good[x] = gd1;
	a[x] += son;
}

int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%d %d", &n, &m);
		Init();

		for (int i = 1; i <= n; i++)
			sc("%lld", &a[i]);
		for (int i = 1; i <= n; i++)
			sc("%lld", &h[i]);

		for (int i = 1; i < n; i++) {
			int u, v; 
			sc("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}

		DFS(1, 0);
		if (ok)
			puts("YES");
		else
			puts("NO");
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

D. Captain Flint and Treasure

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...

There are two arrays aa and bb of length nn. Initially, an ansans is equal to 00 and the following operation is defined:

  1. Choose position ii (1≤i≤n1≤i≤n);
  2. Add aiai to ansans;
  3. If bi≠−1bi≠−1 then add aiai to abiabi.

What is the maximum ansans you can get by performing the operation on each ii (1≤i≤n1≤i≤n) exactly once?

Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

Input

The first line contains the integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of arrays aa and bb.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106).

The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n or bi=−1bi=−1).

Additional constraint: it's guaranteed that for any ii (1≤i≤n1≤i≤n) the sequence bi,bbi,bbbi,…bi,bbi,bbbi,… is not cyclic, in other words it will always end with −1−1.

Output

In the first line, print the maximum ansans you can get.

In the second line, print the order of operations: nn different integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). The pipi is the position which should be chosen at the ii-th step. If there are multiple orders, print any of them.

Examples

input

Copy

3
1 2 3
2 3 -1

output

Copy

10
1 2 3 

input

Copy

2
-1 100
2 -1

output

Copy

99
2 1 

input

Copy

10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9

output

Copy

-9
3 5 6 1 9 4 10 7 8 2

Main idea:

 There are two arrays a and b. The a array represents the weight of the position, and the b array represents the next index of the position (-1 means there is no next index). You have to select the number of each position once. When you choose For the number of position i, the weight of the point index must be added to the weight of the current point. The order of selecting the number is arbitrary, and the maximum weight that can be constructed and the method of construction are output.

solution:

It is not difficult to see that if the weight of a position is positive, it must be selected first, and if it is negative, its index must be selected before the position. But you can’t build edges directly, because there may be a situation where it was originally a negative number, but it can be added to a positive number by other points, so you need to reverse build the edge and use DFS to preprocess the maximum weight that each point may get, and then follow The first idea is to build edges, and finally topological sorting is enough.

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

vector <int> G[N], g[N];
int in[N], b[N], n;
ll a[N], dp[N];
bool vis[N];

void DFS(int x) {
	dp[x] = a[x];
	vis[x] = true;
	for (auto v : g[x]) {
		if (!vis[v])
			DFS(v);
		if (dp[v] > 0)
			dp[x] += dp[v];
	}
}
void Topsort() {
	queue <int> q;
	for (int i = 1; i <= n; i++) {
		if (!in[i])
			q.push(i);
	}

	ll ans = 0;
	vector <int> vec;

	while (!q.empty()) {
		int u = q.front();
		q.pop();

		vec.push_back(u);
		ans += a[u];

		if (b[u] != -1)
			a[b[u]] += a[u];

		for (auto v : G[u]) {
			in[v]--;
			if (!in[v])
				q.push(v);
		}
	}

	printf("%lld\n", ans);
	for (auto v : vec)
		printf("%d ", v);
	puts("");
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		sc("%lld", &a[i]);
	for (int i = 1; i <= n; i++) {
		sc("%d", &b[i]);
		if (b[i] != -1)
			g[b[i]].push_back(i);
	}

	for (int i = 1; i <= n; i++) 
		if (!vis[i])
			DFS(i);

	for (int i = 1; i <= n; i++) {
		if (b[i] == -1)
			continue;
		if (dp[i] > 0)
			G[i].push_back(b[i]), in[b[i]]++;
		else if (dp[i] < 0)
			G[b[i]].push_back(i), in[i]++;
	}

	Topsort();
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/107823913