本文主要是介绍Codeforces1436 A. Reorder,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
For a given array 𝑎 consisting of 𝑛 integers and a given integer 𝑚 find if it is possible to reorder elements of the array 𝑎 in such a way that ∑𝑛𝑖=1∑𝑛𝑗=𝑖𝑎𝑗𝑗 equals 𝑚? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 52=2.5.
Input
The first line contains a single integer 𝑡 — the number of test cases (1≤𝑡≤100). The test cases follow, each in two lines.
The first line of a test case contains two integers 𝑛 and 𝑚 (1≤𝑛≤100, 0≤𝑚≤106). The second line contains integers 𝑎1,𝑎2,…,𝑎𝑛 (0≤𝑎𝑖≤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.
Example
inputCopy
2
3 8
2 5 1
4 4
0 1 2 3
outputCopy
YES
NO
Note
In the first test case one of the reorders could be [1,2,5]. The sum is equal to (11+22+53)+(22+53)+(53)=8. The brackets denote the inner sum ∑𝑛𝑗=𝑖𝑎𝑗𝑗, while the summation of brackets corresponds to the sum over 𝑖.
思路:
等价于所有数的和
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;int main() {int T;scanf("%d",&T);while(T--) {int n,m;scanf("%d%d",&n,&m);int sum = 0;for(int i = 1;i <= n;i++) {int x;scanf("%d",&x);sum += x;}if(sum == m) printf("YES\n");else printf("NO\n");}return 0;
}
这篇关于Codeforces1436 A. Reorder的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!