Codeforce Problem 1436A Recorder 数学

A. Reorder
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ∑ni=1∑nj=iajj equals m? 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 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.

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 ∑nj=iajj, while the summation of brackets corresponds to the sum over i.

题意

给你n个数,能不能找到一个排序,使得在这里插入图片描述
的值等于m,可以输出YES,不可以输出NO

思路

我以为是顺着题目意思写结果WA一次,TLE两次
WA是直接sort排序了
TLE是加了next_permutation
然后发现其实是判断n个数的值等不等于m就行

Code

#include"iostream"
#include"cstdio"
#include"stdlib.h"
#include"cstring"
#include"cstdlib"
#include"vector"
#include"map"
#include"algorithm"
#include <iomanip>
#include <time.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include<bits/stdc++.h>
//const double PI=acos(-1.0);
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define inf 0x3f3f3f3f
#define pr pair<int,int>
#define ios ios::sync_with_stdio(false);
typedef pair<char,int> PAIR;
struct CmpByValue
{
    bool operator()(const PAIR& lhs, const PAIR& rhs)
    {
        return lhs.second > rhs.second;
    }
};
//priority_qe<int,vector<int>,greater<int>> pp;
const int mod = 1000000007;
const int M = 1e5 +100 ;
const int limt = 1<<20;
const int INF = 1e9;
typedef long long ll;
int mp[M];
double sum;
bool flag;
ll t,s,n,m,ans,cnt;
int main()
{
    ios;
    cin>>t;
    ll k=1;
    while(t--)
    {
        cnt=0;
        cin>>n>>m;
        for(int i=1; i<=n; i++)
            cin>>mp[i],cnt+=mp[i];
 
        if(cnt==m)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值