寒假养成计划——Day3

A题

题目链接

Polycarp got an array of integers a[1…n] as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become a1=a2=⋯=an).

  • In one operation, he can take some indices in the array and increase the elements of the array at those indices by 1.

For example, let a=[4,2,1,6,2]. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by 1. As a result, in one operation, he can get a new state of the array a=[5,3,1,7,2].

What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become a1=a2=⋯=an)?

Input

The first line of the input contains a single integer t (1≤t≤10^{4})  — the number of test cases in the test.

The following are descriptions of the input test cases.

The first line of the description of each test case contains one integer nn (1≤n≤50)  — the array a.

The second line of the description of each test case contains n integers a1,a2,…,an (1≤ai≤10^{9})  — elements of the array a.

Output

For each test case, print one integer  — the minimum number of operations to make all elements of the array a equal.

Example

input

3
6
3 4 2 4 1 2
3
1000 1002 998
2
12 11

output

3
4
1

Note

First test case:

  • a=[3,4,2,4,1,2] take a3,a5 and perform an operation plus one on them, as a result we get a=[3,4,3,4,2,2].
  • a=[3,4,3,4,2,2] we take a1,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,3,4,3,3].
  • a=[4,4,3,4,3,3] we take a3,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,4,4,4,4].

There are other sequences of 3 operations, after the application of which all elements become equal.

Second test case:

  • a=[1000,1002,998] 2 times we take a1,a3 and perform an operation plus one on them, as a result we get a=[1002,1002,1000].
  • a=[1002,1002,1000] also take a3 2 times and perform an operation plus one on it, as a result we get a=[1002,1002,1002].

Third test case:

  • a=[12,11] take a2 and perform an operation plus one on it, as a result we get a=[12,12].

题解:

        这道题可以算是非常简单了,题目大意是说给定一个数字序列,每次可以指定任意数量的元素进行+1操作,问能使整个序列元素相同的最小操作次数是多少。简单来说,实际上就是数组中最大元素与最小元素之差。 

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d",&n);
		int maxx=0,minn=1e9+1;
		for (int i=1;i<=n;i++)
		{
			int z;
			scanf("%d",&z);
			maxx=max(z,maxx);
			minn=min(z,minn);
		}
		printf("%d\n",maxx-minn);
	}
	return 0;
}

B题

题目链接

Polycarp has 3 positive integers a, b and c. He can perform the following operation exactly once.

  • Choose a positive integer m and multiply exactly one of the integers a, b or c by m.

Can Polycarp make it so that after performing the operation, the sequence of three numbers a, b, c (in this order) forms an arithmetic progression? Note that you cannot change the order of a, b and c.

Formally, a sequence x1,x2,…,xn is called an arithmetic progression (AP) if there exists a number d (called "common difference") such that x_{i+1}=x_{i}+d for all i from 1 to n−1. In this problem, n=3.

For example, the following sequences are AP: [5,10,15], [3,2,1], [1,1,1], and [13,10,7]. The following sequences are not AP: [1,2,4], [0,1,0] and [1,3,2].

You need to answer t independent test cases.

Input

The first line contains the number t (1≤t≤10^{4}) — the number of test cases.

Each of the following tt lines contains 3 integers a, b, c (1≤a,b,c≤10^{8}).

Output

For each test case print "YES" (without quotes) if Polycarp can choose a positive integer m and multiply exactly one of the integers a, b or c by m to make [a,b,c] be an arithmetic progression. Print "NO" (without quotes) otherwise.

You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).

Example

input

11
10 5 30
30 5 10
1 2 3
1 6 3
2 6 3
1 1 1
1 1 2
1 1 3
1 100000000 1
2 1 1
1 2 2

output

YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES

Note

In the first and second test cases, you can choose the number m=4 and multiply the second number (b=5) by 4.

In the first test case the resulting sequence will be [10,20,30]. This is an AP with a difference d=10.

In the second test case the resulting sequence will be [30,20,10]. This is an AP with a difference d=−10.

In the third test case, you can choose m=1 and multiply any number by 1. The resulting sequence will be [1,2,3]. This is an AP with a difference d=1.

In the fourth test case, you can choose m=9 and multiply the first number (a=1) by 9. The resulting sequence will be [9,6,3]. This is an AP with a difference d=−3.

In the fifth test case, it is impossible to make an AP.


题解:

        这个题也比较简单,大意是说给了三个数a,b,c,可以对这三个数中任意一个数进行×m的操作,问能否是这三个数成为一个等差数列。这道题暴力判断即可。当2b=ac时,一定满足;当2b<ac且ac%2b=0时,也满足;当2b-a>c且(2b-a)%c=0时,满足;当2b-c>a且(2b-c)%a=0时,满足。

        接下来稍微说一下后面的情况。举个简单的例子:a=b=c=1,这个时候2b=a+c的,令b=2,则序列变为1,2,1,这个时候令a或c扩大成原来的3倍,即可满足等差数列的条件,这也就是为什么不能直接说2b>ac一定不能构成等差数列。 

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if (2*b==a+c)
			puts("YES");
		else if (2*b<a+c&&(a+c)%(2*b)==0)
			puts("YES");
		else if (2*b-a>c&&(2*b-a)%c==0)
			puts("YES");
		else if (2*b-c>a&&(2*b-c)%a==0)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}

C题

题目链接

You are given an array a consisting of n positive integers. You can perform operations on it.

In one operation you can replace any element of the array ai with ⌊\frac{x_i}{2}⌋, that is, by an integer part of dividing aiai by 2 (rounding down).

See if you can apply the operation some number of times (possible 0) to make the array a become a permutation of numbers from 1 to n —that is, so that it contains all numbers from 1 to n, each exactly once.

For example, if a=[1,8,25,2], n=4, then the answer is yes. You could do the following:

  1. Replace 8 with ⌊\frac{8}{2}⌋=4, then a=[1,4,25,2].
  2. Replace 25 with ⌊\frac{25}{2}⌋=12, then a=[1,4,12,2].
  3. Replace 12 with ⌊\frac{12}{2}⌋=6, then a=[1,4,6,2].
  4. Replace 6 with ⌊\frac{6}{2}⌋=3, then a=[1,4,3,2].

Input

The first line of input data contains an integer t (1≤t≤10^{4}) —the number of test cases.

Each test case contains exactly two lines. The first one contains an integer nn (1≤n≤50), the second one contains integers a1,a2,…,an (1≤ai≤10^{9}).

Output

For each test case, output on a separate line:

  • YES if you can make the array a become a permutation of numbers from 1 to n,
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Example

input

6
4
1 8 25 2
2
1 1
9
9 8 3 4 2 7 1 5 6
3
8 2 1
4
24 7 16 7
5
22 6 22 4 22

output

YES
NO
YES
NO
NO
YES

Note

The first test case is explained in the text of the problem statement.

In the second test case, it is not possible to get a permutation.


题解:

        这道题也比较容易,题目大意是说给定n个数,可以选择进行÷2的操作,使得整个序列变成拥有1~n序列的集合。这道题就是它怎么说就怎么做就好,对于大于n的数字而言,一直进行÷2操作,直到这个数在1~n范围内且仅出现一次,如果没有则输出NO,全都有则输出YES。

        这个题做的时候注意一点,开始的时候最好排个序(从大到小),这样避免循环停止而输出错误结果带来的影响(第六个testcase)。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int vis[55],a[55];
bool cmp(int a,int b)
{
	return a>b;
}

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		memset(vis,0,sizeof(vis));
		scanf("%d",&n);
		for (int i=0;i<n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n,cmp);
		for (int i=0;i<n;i++)
		{
			int z=a[i];
			while (z>n)
				z/=2;
			while (vis[z]==1)
				z/=2;
			if (z!=0)
				vis[z]=1;
			else
				break;			
		}
		bool f=1;
		for (int i=1;i<=n;i++)
		{
			if (vis[i]!=1)
			{
				f=0;
				break;
			}
		}
		cout<<(f?"YES":"NO")<<endl;
	}
	return 0;
}

ps:本人实力较菜,只能做出这些简单题,后面的题如果有哪位大神可以解决可以私信我,如果这里面有讲的不清楚或者有错误的,望及时指出!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值