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.

题目大意:

输入一个数N,输出四个不同的数,其中有三个是近似素数(两个素数的乘积),使这四个数的和等于N。

解法:

找到前三个近似素数,即6,10,14,看看N减去这三个数是否大于0,然后再判断一下减完后的数是不是和其中一个数相等,如果是的话,把14换成15,减完剩下的数-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.

题目大意:

输入一个数N,你要构造一个数,他的位数是N,把它的二进制数去掉末尾N个,剩下的二进制串要尽量大,如果有多个这样的数,输出最小的那个。

解法:

四位二进制数只有8和9,所以用8,9来构造,看一下去掉的N个二进制数占了多少个四位,前面的填9,后面的填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

题目大意:

 一棵N个点的树,每个点的第一个权值表示多少居民住这(可能没有),所有人初始都在1,他们要从1走到各自住的地方,刚开始每个人要么心情好要么不好,在回家的路上,心情只可能从好->不好,每个点第二个权值表示经过该点回家心情好的人-心情不好的人,输出给出的条件是否合法。

解法:

已知每个点子树中居民的大小和该点的权值,那么情况就是唯一的,DFS一遍回溯的时候判断是否合法就行了。主要判断的东西有两个:1、居民是否够用 2、当前子树的居民好坏数量能否构造成该点的权值 。构造的时候注意子树中坏心情是可以变成当前点的好心情的。

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

题目大意:

 两个数组a和b,a数组表示位置的权值,b数组表示该位置的下一个索引(-1即没有下一个索引),你要把每个位置的数都选一次,当你选了位置i的数,该点索引的权值要加上当前点的权值,选数的顺序随意,输出可以构造出来的最大权值和构造的方法。

解法:

不难看出,一个位置的权值如果是正的话,肯定得先选,如果是负的,他的索引肯定比该位置先选。但是不能直接建边,因为可能存在一种情况,他原来是负数,却可以被其他点加成正数,所以要反向建边用DFS预处理出每个点可能得到的最大权值,再按照最开始的思路建边,最后拓扑排序即可。

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记得改宏定义!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/107823913
今日推荐