Codeforces暑期训练周报(7.28~8.3)

B. Fedor and New Game

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».

The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer x_i. Consider binary representation of x_i: if the j-th bit of number x_i equal to one, then the army of the i-th player has soldiers of the j-th type.

Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.

Input

The first line contains three integers nmk (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).

The i-th of the next (m + 1) lines contains a single integer x_i (1 ≤ x_i ≤ 2^n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player. 

Output

Print a single integer — the number of Fedor's potential friends.

Examples

input

7 3 1
8
5
111
17

output

0

input

3 3 3
1
2
3
4

output

3

题解:

        一道很让人“费解”的题目,它的任务是要判断输入的最后一个数与前面的数作比较,如果说它们的二进制相差位数(即不同的位数)如果小于等于k,则它们可以成为朋友。比较容易想到的做法是异或运算(其他的以异或运算为基础的暴力也可),判断这个异或值数字为1的个数(基于异或运算的特点,相同为0,不同为1),如果小于等于k则ans++。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
using namespace std;

const int N=1e3+5;
int a[N];
void solve(){
	int n,m,k;
	cin>>n>>m>>k;
	for (int i=0;i<=m;i++){
		cin>>a[i];
	}
	int f=a[m];
	int ans=0;
	for (int i=0;i<m;i++){
		int p=f^a[i];
		int b=0;
		while (p){
			if (p&1){
				b++;
			}
			p>>=1;
		}
		if (b<=k){
			ans++;
		}
	}
	cout<<ans<<endl;
}
int main()
{
	BUFF;
	int _;
	//cin>>_;
	_=1; 
	while (_--){
		solve();
	}
	return 0;
}


C. Two Teams Composing

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You have n students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the i-th student skill is denoted by an integer a_i (different students can have the same skills).

So, about the teams. Firstly, these two teams should have the same size. Two more constraints:

  • The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).
  • The second team should consist of students with the same skills (i.e. all skills in the second team are equal).

Note that it is permissible that some student of the first team has the same skill as a student of the second team.

Consider some examples (skills are given):

  • [1,2,3], [4,4] is not a good pair of teams because sizes should be the same;
  • [1,1,2], [3,3,3] is not a good pair of teams because the first team should not contain students with the same skills;
  • [1,2,3], [3,4,4] is not a good pair of teams because the second team should contain students with the same skills;
  • [1,2,3], [3,3,3] is a good pair of teams;
  • [5], [6] is a good pair of teams.

Your task is to find the maximum possible size x for which it is possible to compose a valid pair of teams, where each team size is x (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t10^4) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅10^5) — the number of students. The second line of the test case contains n integers a_1,a_2,...,a_n (1≤a_in), where a_i is the skill of the i-th student. Different students can have the same skills. 

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5 (∑n≤2⋅10^5).  

Output

For each test case, print the answer — the maximum possible size x for which it is possible to compose a valid pair of teams, where each team size is x.

Example

input

4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3

output

3
1
0
2

Note

In the first test case of the example, it is possible to construct two teams of size 3: the first team is [1,2,4] and the second team is [4,4,4]. Note, that there are some other ways to construct two valid teams of size 3.

题解:

        一道很好的模拟题。这个题主要是要分类讨论,即最大的相同元素个数与不同元素个数的比较,显然如果前者小于后者,则答案即为前者;如果二者相同,则答案为前者-1(参照第一个样例);如果前者大于后者,则答案为后者。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
using namespace std;

const int N=2e5+5;
int a[N],b[N];
void solve(){
	int n;
	cin>>n;
	memset(b,0,sizeof(b));
	set<int> s;
	for (int i=1;i<=n;i++){
		cin>>a[i];
		b[a[i]]++;
		s.insert(a[i]);
	}
	int ans=*max_element(b+1,b+1+n);
	int cnt=s.size();
	if (ans<cnt){
		cout<<ans<<endl;
	}
	else if (ans==cnt){
		cout<<ans-1<<endl;
	}
	else{
		cout<<cnt<<endl;
	}
}
int main()
{
	BUFF;
	int _;
	cin>>_;
	//_=1; 
	while (_--){
		solve();
	}
	return 0;
}

A. Odd Selection

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.

Tell him whether he can do so.

Input

The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n and x (1≤xn≤1000) — the length of the array and the number of elements you need to choose.

The next line of each test case contains n integers a_1,a_2,...,a_n (1≤a_i≤1000) — elements of the array.

Output

For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.

You may print every letter in any case you want.

Example

input

5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103

output

Yes
No
Yes
Yes
No

Note

For 1st case: We must select element 999, and the sum is odd.

For 2nd case: We must select element 1000, so overall sum is not odd.

For 3rd case: We can select element 51.

For 4th case: We must select both elements 50 and 51  — so overall sum is odd.

For 5th case: We must select all elements  — but overall sum is not odd.

题解:

        题面很简单,让你选择x个数,使得他们的和是奇数。我们可以先假定选出一个奇数出来,然后再剩下的数字里凑出偶数,如果可以凑出来就是YES,否则就是NO。需要注意全偶数的情况,要单独判断一下。

AC代码: 

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
using namespace std;

const int N=1e3+5;
int a[N]; 
void solve(){
	int n,x;
	cin>>n>>x;
	int odd=0,even=0;
	for (int i=0;i<n;i++){
		cin>>a[i];
		a[i]=a[i]%2;
		if (a[i]==0){
			even++;
		}
		else{
			odd++;
		}
	}
	odd--,x--;
	if (odd==-1){
		cout<<"No"<<endl;
		return;
	}
	if (x<=even){
		cout<<"Yes"<<endl;
		return;
	}
	while (odd>=2&&x>=2){
		odd-=2,x-=2;
	}
	if (even>=x){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
}
int main()
{
	BUFF;
	int _;
	cin>>_;
	//_=1; 
	while (_--){
		solve();
	}
	return 0;
}

D. Same Differences

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and a_j-a_i=j-i.

Input

The first line contains one integer t (1≤t10^4). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅10^5).

The second line of each test case contains n integers a_1,a_2,...,a_n (1≤a_in) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

Output

For each test case output the number of pairs of indices (i,j) such that i<j and a_j-a_i=j-i

Example

input

4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6

output

1
3
3
10

题解:

        比较坑的题目,有两个点:第一,开long long,也是非常重要的事情,int确实会爆;第二,如果单纯的看样例,会很自然地认为答案是k*(k-1)/2,k为a[i]=i的个数,实际上还要考虑很多的情况,即只要满足ai-i=aj-j就行,所以我们用一个map去记录相同的个数,最后再用k*(k-1)/2逐个相加就是答案。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
using namespace std;

const int N=2e5+5;
ll a[N]; 
void solve(){
	map<ll,ll> mp;
	int n;
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>a[i];
		mp[a[i]-i]++;
	}
	ll ans=0;
	for (int i=-n;i<=n;i++){
		ans+=(mp[i]*(mp[i]-1))/2;
	}
	cout<<ans<<endl;
}
int main()
{
	BUFF;
	int _;
	cin>>_;
	//_=1; 
	while (_--){
		solve();
	}
	return 0;
}

C. Similar Pairs

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

We call two numbers x and y similar if they have the same parity (the same remainder when divided by 2), or if |x−y|=1. For example, in each of the pairs (2,6), (4,3), (11,7), the numbers are similar to each other, and in the pairs (1,4), (3,12), they are not.

You are given an array a of n (n is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.

For example, for the array a=[11,14,16,12], there is a partition into pairs (11,12) and (14,16). The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.

Input

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

Each test case consists of two lines.

The first line contains an even positive integer n (2≤n≤50) — length of array aa.

The second line contains nn positive integers a_1,a_2,...,a_n (1≤a_i≤100).

Output

For each test case print:

  • YES if the such a partition exists,
  • NO otherwise.

The letters in the words YES and NO can be displayed in any case.

Example

input

7
4
11 14 16 12
2
1 8
4
1 1 1 1
4
1 2 5 6
2
12 13
6
1 6 3 10 5 8
6
1 12 3 10 5 8

output

YES
NO
YES
YES
YES
YES
NO

Note

The first test case was explained in the statement.

In the second test case, the two given numbers are not similar.

In the third test case, any partition is suitable.

题解:

        一道比较简单的题目,题目要求让你输出这个序列是否都是由好的配对组成,题目很明确地表明了n是偶数,所以在这个序列中奇数和偶数的奇偶性是相同的。显然若二者的个数均为偶数,答案就是YES,否则,则看序列中能否找到一对相差为一的配对。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
using namespace std;

const int N=50+5;
ll a[N];
void solve(){
	int n;
	cin>>n;
	int even=0,odd=0;
	for (int i=0;i<n;i++){
		cin>>a[i];
		if (a[i]&1){
			odd++;
		}
		else{
			even++;
		}
	}
	if (odd%2==0&&even%2==0){
		cout<<"YES"<<endl;
		return;
	}
	sort(a,a+n);
	bool f=0;
	for (int i=1;i<n;i++){
		if (a[i]-a[i-1]==1){
			f=1;
			break;
		}
	}
	cout<<(f?"YES":"NO")<<endl;
}
int main()
{
	BUFF;
	int _;
	cin>>_;
	//_=1; 
	while (_--){
		solve();
	}
	return 0;
}

B. Same Parity Summands

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given two positive integers n (1≤n10^9) and k (1≤k≤100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 2).

In other words, find a_1,a_2,...,a_k such that all a_i>0n=a_1+a_2+...+a_k and either all a_i are even or all a_i are odd at the same time.

If such a representation does not exist, then report it.

Input

The first line contains an integer (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.

Each test case is two positive integers n (1≤n10^9) and k (1≤k≤100).

Output

For each test case print:

  • YES and the required values aiai, if the answer exists (if there are several answers, print any of them);
  • NO if the answer does not exist.

The letters in the words YES and NO can be printed in any case.

Example

input

8
10 3
100 4
8 7
97 2
8 8
3 10
5 3
1000000000 9

output

YES
4 2 4
YES
55 5 5 35
NO
NO
YES
1 1 1 1 1 1 1 1
NO
YES
3 1 1
YES
111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120

题解:

        一道比较简单的贪心数学题,题目问用k个奇偶性相同的数字相加之后能否等于n,如果可以,则输出一个可行的序列。不难想到我们可以把问题简单化,即k-1个数要么由1组成,要么由2组成,分别对应了奇数和偶数的情况,那么我们就可以直接判断最后一个数的奇偶性是否满足要求就可以了,不满足就是NO,满足就是YES,再输出一下序列的值即可。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
using namespace std;

const int N=50+5;
ll a[N];
void solve(){
	int n,k;
	cin>>n>>k;
	if (n-k>=0&&(n-k)%2==0){
		cout<<"YES"<<endl;
		for (int i=0;i<k-1;i++){
			cout<<"1 ";
		}
		cout<<n-k+1<<endl;
	}
	else if (n-2*k>=0&&(n-2*k)%2==0){
		cout<<"YES"<<endl;
		for (int i=0;i<k-1;i++){
			cout<<"2 ";
		}
		cout<<n-2*k+2<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
}
int main()
{
	BUFF;
	int _;
	cin>>_;
	//_=1; 
	while (_--){
		solve();
	}
	return 0;
}

A. Chewbaсca and Number

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t.

Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.

Input

The first line contains a single integer x (1 ≤ x ≤ 10^{18}) — the number that Luke Skywalker gave to Chewbacca.

Output

Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.

Examples

input

27

output

22

input

4545

output

4444

题解:

        一道非常简单的1200分的题目,只需要对每一位数取min(t,9-t)就可以,为了方便描述,可以拿字符串去存这个数,这样直接判断就行。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
using namespace std;

void solve(){
	string s;
	cin>>s;
	F(i,0,s.length()){
		if (i==0&&s[i]=='9'){
			continue;
		}
		if (s[i]-'0'>=5){
			s[i]=9-(s[i]-'0')+'0';
		}
	}
	cout<<s<<endl;
}
int main()
{
	BUFF;
	int _;
	//cin>>_;
	_=1; 
	while (_--){
		solve();
	}
	return 0;
}

A. Double Cola

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.

For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.

Write a program that will print the name of a man who will drink the n-th can.

Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.

Input

The input data consist of a single integer n (1 ≤ n ≤ 10^9).

It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.

Output

Print the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.

Examples

input

1

output

Sheldon

input

6

output

Sheldon

input

1802

output

Penny

题解:

        一道等比数列的找规律题,不难发现,整个队列的过程是以5为基数,2为公比的序列增长的,所以我们需要判断一下当前的n在哪个组里,根据组号k确定n这个位置上的人是谁。

AC代码:

#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
using namespace std;

void solve(){
	int n;
	cin>>n;
	int k=0;
	while (5*(pow(2,k)-1)<n){
		k++;
	}
	n-=5*(pow(2,k-1)-1);
	int idx=ceil(n/pow(2,k-1));
	if (idx==1){
		cout<<"Sheldon"<<endl;
	}
	else if (idx==2){
		cout<<"Leonard"<<endl;
	}
	else if (idx==3){
		cout<<"Penny"<<endl;
	}
	else if (idx==4){
		cout<<"Rajesh"<<endl;
	}
	else if (idx==5){
		cout<<"Howard"<<endl;
	}
}
int main()
{
	BUFF;
	int _;
	//cin>>_;
	_=1; 
	while (_--){
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值