cf #760 div3

A. Polycarp and Sums of Subsequences

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp had an array aa of 33 positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array bb of 77 integers.

For example, if a={1,4,3}a={1,4,3}, then Polycarp wrote out 11, 44, 33, 1+4=51+4=5, 1+3=41+3=4, 4+3=74+3=7, 1+4+3=81+4+3=8. After sorting, he got an array b={1,3,4,4,5,7,8}.b={1,3,4,4,5,7,8}.

Unfortunately, Polycarp lost the array aa. He only has the array bb left. Help him to restore the array aa.

Input

The first line contains one integer tt (1≤t≤50001≤t≤5000) — the number of test cases.

Each test case consists of one line which contains 77 integers b1,b2,…,b7b1,b2,…,b7 (1≤bi≤1091≤bi≤109; bi≤bi+1bi≤bi+1).

Additional constraint on the input: there exists at least one array aa which yields this array bb as described in the statement.

Output

For each test case, print 33 integers — a1a1, a2a2 and a3a3. If there can be several answers, print any of them.

Example

input

5
1 3 4 4 5 7 8
1 2 3 4 5 6 7
300000000 300000000 300000000 600000000 600000000 600000000 900000000
1 1 2 999999998 999999999 999999999 1000000000
1 2 2 3 3 4 5

output

1 4 3
4 1 2
300000000 300000000 300000000
999999998 1 1
1 2 2

Note

The subsequence of the array aa is a sequence that can be obtained from aa by removing zero or more of its elements.

Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length 33 has exactly 77 different non-empty subsequences.

ac代码:

#include <stdio.h>
int t;
long num[7];

int main(){
	int i;
	scanf("%d",&t);
	while(t--){
		for(i=0;i<7;i++)scanf("%ld",&num[i]);
		printf("%ld %ld ",num[0],num[1]);
		printf("%ld\n",(num[6]-num[0]-num[1]));
	}	
	return 0;
} 

B. Missing Bigram

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has come up with a new game to play with you. He calls it "A missing bigram".

A bigram of a word is a sequence of two adjacent letters in it.

For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".

The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard.

Finally, Polycarp invites you to guess what the word that he has come up with was.

Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with.

The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

Input

The first line contains a single integer tt (1≤t≤20001≤t≤2000) — the number of testcases.

The first line of each testcase contains a single integer nn (3≤n≤1003≤n≤100) — the length of the word Polycarp has come up with.

The second line of each testcase contains n−2n−2 bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'.

Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists.

Output

For each testcase print a word, consisting of nn letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with.

The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

Example

input

4
7
ab bb ba aa ba
7
ab ba aa ab ba
3
aa
5
bb ab bb

output

abbaaba
abaabaa
baa
bbabb

Note

The first two testcases from the example are produced from the word "abbaaba". As listed in the statement, it contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".

In the first testcase, the 55-th bigram is removed.

In the second testcase, the 22-nd bigram is removed. However, that sequence could also have been produced from the word "abaabaa". It contains bigrams "ab", "ba", "aa", "ab", "ba" and "aa". The missing bigram is the 66-th one.

In the third testcase, all of "baa", "aab" and "aaa" are valid answers.

#include <stdio.h>
int t,n,k;
char a,b;
char str[110];
void renew(){
	int i;
	for(i=0;i<101;i++){
		str[i] = '\0';
	}
}
int main(){
	int i=0;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		renew();
		getchar();
		k = n-2;
		scanf("%c%c",&a,&b);
		getchar();
		str[0] = a;str[1] = b;
		k--;i=1;
		while(k--){
			scanf("%c%c",&a,&b);
				getchar();
			if(str[i]==a){
				str[++i] = b;
			}	
			else{
				str[++i] = a;
				str[++i] = b;
			}
		}
		printf("%s",str);
		n-=i+1;
		while(n--){
			printf("a");
		}
		printf("\n");
	}	
	return 0;
} 

C. Paint the Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn positive integers. You have to choose a positive integer dd and paint all elements into two colors. All elements which are divisible by dd will be painted red, and all other elements will be painted blue.

The coloring is called beautiful if there are no pairs of adjacent elements with the same color in the array. Your task is to find any value of dd which yields a beautiful coloring, or report that it is impossible.

Input

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

The first line of each testcase contains one integer nn (2≤n≤1002≤n≤100) — the number of elements of the array.

The second line of each testcase contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10181≤ai≤1018).

Output

For each testcase print a single integer. If there is no such value of dd that yields a beautiful coloring, print 00. Otherwise, print any suitable value of dd (1≤d≤10181≤d≤1018).

Example

input

5
5
1 2 3 4 5
3
10 5 15
3
100 10 200
10
9 8 2 6 6 2 8 6 5 4
2
1 3

output

2
0
100
0
3

 题意理解:

        找到一个数d,将a中能够整除于d的数涂成一个颜色,其他的数涂成另外一个颜色。要使得涂色之后,数组内相邻的两个数颜色不相同。如果存在这样的一个数d,则输出d,否则输出0

思路:

        可以将数组的数分为奇数偶数

        假设d存在于奇数位置的数。由于奇数位置的数都能整除于d,所以d是奇数位置的数的公约数,同时,要尽量使得偶数位置的数没办法整除于d

        因此,d的因子越多越好——即,d是奇数位置的数的最大公约数。

        找出来之后,d已经确定,只需要排查是否偶数位置的数能够被d整除。能则无解,否则输出d。

#include <stdio.h>
int t,n;
long long a[105]; 
void renew(){
	int i;
	for(i=0;i<105;i++)
		a[i] = 0;
}
//找出最大公约数
long long gcd(long long a,long long b){
	return a%b==0?b:gcd(b,a%b);
}
//check函数:检查奇数or偶数位置上是否存在能被d整除的数
int check(int c, long long b, long long a[]){
	int i;
	for(i = c;i<n;i+=2){
		if(a[i]%b==0){
			return 0;
		} 
	}
	return 1;
}
int main(){
	int i;
	long long a1,b;
	scanf("%d",&t);
	while(t--){
		renew();
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%lld",&a[i]);
        //分情况讨论
		if(n>=4){
			a1 = gcd(a[0],a[2]);
			b = gcd(a[1],a[3]);
		}
		else if(n==3){
			a1 = gcd(a[0],a[2]);
			b= a[1];
		}
		else {
			a1 = a[0];
			b = a[1];
		}
        //当数据大于4个的时候,遍历查找最大公约数
		for(i=4;i<n;i+=2){
			a1 = gcd(a1,a[i]);
			if(i+1<n)
				b = gcd(b,a[i+1]);
		}
        //假设偶数位置存在最大公约数
		if(check(0,b,a)){
			printf("%lld\n",b);
		}
        //假设奇数位置存在最大公约数
		else if(check(1,a1,a)){
			printf("%lld\n",a1);
		}
        //无解
		else{
			printf("0\n");
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值