Codeforces Round #678 (Div. 2)------Reorder

题目

For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that在这里插入图片描述
It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5 / 2=2.5.

Input
The first line contains a single integer t — the number of test cases (1≤t≤100). The test cases follow, each in two lines.

The first line of a test case contains two integers n and m (1≤n≤100, 0≤m≤106). The second line contains integers a1,a2,…,an (0≤ai≤106) — the elements of the array.

Output
For each test case print “YES”, if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and “NO” otherwise.

样例

//输入:
2
3 8
2 5 1
4 4
0 1 2 3
//输出:
YES
NO

题意

给定一个含有n个元素的数值a,判断是否满足题目中的那个公式,若满足,输出YES,否则输出NO;

题解

公式前几项展开便可发现规律,最后是 在这里插入图片描述
因此,题目简化后就是求数组元素的和是否等于m。

AC代码

#include<iostream>
using namespace std;
int main()
{
	int n, m, t;
	cin >> t;
	while (t--) {
		int sum = 0;
		cin >> n >> m;

		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			sum += x;
		}
		if (sum == m)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
posted @ 2020-10-31 08:23  AC_沫离  阅读(20)  评论(0)    收藏  举报