codeforces题解(800)

目录

A. Optimal Path

A. The Third Three Number Problem

A. XOR Mixup

B. Rising Sand

A. NIT orz!

A. Food for Animals

A. Optimal Path

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a table aa of size n×mn×m. We will consider the table rows numbered from top to bottom from 11 to nn, and the columns numbered from left to right from 11 to mm. We will denote a cell that is in the ii-th row and in the jj-th column as (i,j)(i,j). In the cell (i,j)(i,j) there is written a number (i−1)⋅m+j(i−1)⋅m+j, that is aij=(i−1)⋅m+jaij=(i−1)⋅m+j.

A turtle initially stands in the cell (1,1)(1,1) and it wants to come to the cell (n,m)(n,m). From the cell (i,j)(i,j) it can in one step go to one of the cells (i+1,j)(i+1,j) or (i,j+1)(i,j+1), if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path.

For example, with n=2n=2 and m=3m=3 the table will look as shown above. The turtle can take the following path: (1,1)→(1,2)→(1,3)→(2,3)(1,1)→(1,2)→(1,3)→(2,3). The cost of such way is equal to a11+a12+a13+a23=12a11+a12+a13+a23=12. On the other hand, the paths (1,1)→(1,2)→(2,2)→(2,1)(1,1)→(1,2)→(2,2)→(2,1) and (1,1)→(1,3)(1,1)→(1,3) are incorrect, because in the first path the turtle can't make a step (2,2)→(2,1)(2,2)→(2,1), and in the second path it can't make a step (1,1)→(1,3)(1,1)→(1,3).

You are asked to tell the turtle a minimal possible cost of a path from the cell (1,1)(1,1) to the cell (n,m)(n,m). Please note that the cells (1,1)(1,1) and (n,m)(n,m) are a part of the way.

Input

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

A single line of each test case contains two integers nn and mm (1≤n,m≤1041≤n,m≤104) — the number of rows and columns of the table aa respectively.

Output

For each test case output a single integer — a minimal possible cost of a path from the cell (1,1)(1,1) to the cell (n,m)(n,m).

Example

input

7
1 1
2 3
3 2
7 1
1 10
5 5
10000 10000

output

1
12
13
28
55
85
500099995000

 题意:从(1,1)出发到(n,m)的最小和

n,m不等于1的时候,最小和为第一行的值+m列的值

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   	int t;
   	cin>>t;
   	while(t--)
   	{
   		int n,m;
   		cin>>n>>m;
   		ll tmp,sum;
   		if(n==1||m==1)  //判断行,列为1的情况
		   {
		   	if(m==1&&n==1) sum=1;
		   	if(n==1&&m!=1)
		   	{
		   		 tmp=0;sum=0;
		   	    for(int i=0;i<m;i++)
   	        	{
   				   ++tmp;
   				   sum+=tmp;
	        	}
			}
			if(m==1&&n!=1)
			{
				 tmp=0;sum=0;
		   	   for(int i=0;i<n;i++)
   	        	{
   				   ++tmp;
   			    	sum+=tmp;
		        }
			} 
		   } 
		   else
		   {
		   	     tmp=0;sum=0;
		   		for(int i=0;i<m;i++)
   		        {
   				++tmp;
   				sum+=tmp;
	        	}
	        	for(int i=1;i<n;i++)
		        {
		        	tmp+=m;
		        	sum+=tmp;
		        }
		        
		   }
   	cout<<sum<<endl;
   		
	}
 	return 0;
} 

A. The Third Three Number Problem

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a positive integer nn. Your task is to find any three integers aa, bb and cc (0≤a,b,c≤1090≤a,b,c≤109) for which (a⊕b)+(b⊕c)+(a⊕c)=n(a⊕b)+(b⊕c)+(a⊕c)=n, or determine that there are no such integers.

Here a⊕ba⊕b denotes the bitwise XOR of aa and bb. For example, 2⊕4=62⊕4=6 and 3⊕1=23⊕1=2.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The following lines contain the descriptions of the test cases.

The only line of each test case contains a single integer nn (1≤n≤1091≤n≤109).

Output

For each test case, print any three integers aa, bb and cc (0≤a,b,c≤1090≤a,b,c≤109) for which (a⊕b)+(b⊕c)+(a⊕c)=n(a⊕b)+(b⊕c)+(a⊕c)=n. If no such integers exist, print −1−1.

Example

input

5
4
1
12
2046
194723326

output

3 3 1
-1
2 4 6
69 420 666
12345678 87654321 100000000

随意输出a,b,c满足a^b+a^c+b^c=n,a=0,b=0,c, a^b+a^c+b^c=c*2,所以c=n/2,n必须是偶数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		if(n%2) cout<<-1<<endl;
		else cout<<"0"<<' '<<"0"<<' '<<n/2<<endl;
	}
	return 0;
}

A. XOR Mixup

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an array aa with n−1n−1 integers. Let xx be the bitwise XOR of all elements of the array. The number xx is added to the end of the array aa (now it has length nn), and then the elements are shuffled.

You are given the newly formed array aa. What is xx? If there are multiple possible values of xx, you can output any of them.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤1002≤n≤100) — the number of integers in the resulting array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1270≤ai≤127) — the elements of the newly formed array aa.

Additional constraint on the input: the array aa is made by the process described in the statement; that is, some value of xx exists.

Output

For each test case, output a single integer — the value of xx, as described in the statement. If there are multiple possible values of xx, output any of them.

Example

input

Copy

4
4
4 3 2 5
5
6 1 10 7 10
6
6 6 6 6 6 6
3
100 100 0

output

Copy

3
7
6
0

循环遍历,先挑出1个数,让剩下的n-1个数循环异或,然后再跟挑出的1个数对比看是否相等,输出即可,代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		vector<int> arr(n);
		for(int i=0;i<n;i++)
			cin>>arr[i];
		for(int i=0;i<n;i++)
		{
			 int sum=0;
			for(int j=0;j<n;j++)
			{
				if(j==i)
				continue;
				sum^=arr[j]; 
			}
			if(sum==arr[i])
			{
				cout<<arr[i]<<endl;
				break;
			}
		}
	}
	return 0;
}

B. Rising Sand

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn piles of sand where the ii-th pile has aiai blocks of sand. The ii-th pile is called too tall if 1<i<n1<i<n and ai>ai−1+ai+1ai>ai−1+ai+1. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)

You are given an integer kk. An operation consists of picking kk consecutive piles of sand and adding one unit of sand to them all. Formally, pick 1≤l,r≤n1≤l,r≤n such that r−l+1=kr−l+1=k. Then for all l≤i≤rl≤i≤r, update ai←ai+1ai←ai+1.

What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (3≤n≤2⋅1053≤n≤2⋅105; 1≤k≤n1≤k≤n) — the number of piles of sand and the size of the operation, respectively.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the sizes of the piles.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a single integer — the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.

Example

input

3
5 2
2 9 2 4 1
4 4
1 3 2 1
3 1
1 3 1

output

2
0
1

题意:输入n,k值。再输入长度为n的数组,如果arr[i]>arr[i-1]+arr[i+1],则认为arr[i] 为 too tall

允许多次,连续的k个数的值+1,求最后有多少个 too tall

思路: 当k=1时,两端保留,可以把中间的每隔一个数+1,加成too tall, 共有(n-1)/2个数,直接输出

            当k<1时,要想要让一个数+1,会牵扯到旁边的数,所以直接计算在原始数据中有多少个too tall即可

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+50;
typedef long long ll;
ll arr[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
		}
		if(k==1)
		cout<<(n-1)/2<<endl;
		else
		{
			int ans=0;
			for(int i=1;i<n-1;i++)
			{
				if(arr[i]>arr[i-1]+arr[i+1])
				ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

A. NIT orz!

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to orzor⁡z. Can you solve this problem too?

You are given a 1-indexed array of nn integers, aa, and an integer zz. You can do the following operation any number (possibly zero) of times:

  • Select a positive integer ii such that 1≤i≤n1≤i≤n. Then, simutaneously set aiai to (aiorz)(aior⁡z) and set zz to (aiandz)(aiand⁡z). In other words, let xx and yy respectively be the current values of aiai and zz. Then set aiai to (xory)(xor⁡y) and set zz to (xandy)(xand⁡y).

Here oror and andand denote the bitwise operations OR and AND respectively.

Find the maximum possible value of the maximum value in aa after any number (possibly zero) of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of the test cases follows.

The first line of each test case contains two integers nn and zz (1≤n≤20001≤n≤2000, 0≤z<2300≤z<230).

The second line of each test case contains nn integers a1a1,a2a2,……,anan (0≤ai<2300≤ai<230).

It is guaranteed that the sum of nn over all test cases does not exceed 104104.

Output

For each test case, print one integer — the answer to the problem.

Example

input

5
2 3
3 4
5 5
0 2 4 6 8
1 9
10
5 7
7 15 30 29 27
3 39548743
10293834 10284344 13635445

output

7
13
11
31
48234367

思路:输入n,z,输入n个数,在这几个数中判断x|z之后最大的数,输出这个最大的数。可以算出,每个数只有在第一次|z的时候,数是最大的,所以循环遍历,找出|z最大的数,输出即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e9+50;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		ll z;
		cin>>n>>z;
		int  ans=0,x;
		for(int i=0;i<n;i++)
		{
			cin>>x;
			if((x|z)>=ans)
			ans=x|z;
		}
		cout<<ans<<endl;
	}
	return 0;
}

A. Food for Animals

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In the pet store on sale there are:

  • aa packs of dog food;
  • bb packs of cat food;
  • cc packs of universal food (such food is suitable for both dogs and cats).

Polycarp has xx dogs and yy cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his cats should receive one pack of suitable food for it.

Input

The first line of input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input.

Then tt lines are given, each containing a description of one test case. Each description consists of five integers a,b,c,xa,b,c,x and yy (0≤a,b,c,x,y≤1080≤a,b,c,x,y≤108).

Output

For each test case in a separate line, output:

  • YES, if suitable food can be bought for each of xx dogs and for each of yy cats;
  • NO else.

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

Copy

7
1 1 4 2 3
0 0 0 0 0
5 5 0 4 6
1 1 1 1 1
50000000 50000000 100000000 100000000 100000000
0 0 0 100000000 100000000
1 3 2 2 5

output

Copy

YES
YES
NO
YES
YES
NO
NO
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ll a,b,c,x,y;
		cin>>a>>b>>c>>x>>y;
	     if (a + b + c >= x + y && a + c >= x && b + c >= y)
            cout << "YES" << '\n';
        else cout << "NO" << '\n';
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落拾一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值